C# 无法将类型“System.Collections.Generic.List<T>”隐式转换为“System.Linq.IQueryable<T>”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10516086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:09:49  来源:igfitidea点击:

Cannot implicitly convert type 'System.Collections.Generic.List<T>' to 'System.Linq.IQueryable<T>'

c#visual-studio-2010linq-to-entitieshierarchical-datasilverlight-5.0

提问by xhedgepigx

I am trying to create a query in my domain service (VS 2010 Silverlight Business Application) that returns the results from inspection readings that came out as a specific value, my database is set up as:

我正在尝试在我的域服务(VS 2010 Silverlight 业务应用程序)中创建一个查询,该查询返回作为特定值出现的检查读数的结果,我的数据库设置为:

Locations
  a) Inspections
     b) InspectionItems
        c) InspectionReadings       
  a) Areas
     b) Inspections
        c) InspectionItems
           d) InspectionReadings

So, as you can see, there are inspection readings for locations under areas and locations. I have a POCO called name StatusList:

因此,如您所见,区域和位置下的位置有检查读数。我有一个名为 StatusList 的 POCO:

    public class StatusList
    {
        [Key]
        [Editable(false)]
        public Guid ID { get; set; }

        public string LocationName { get; set; }

        public DateTime LastInspectionDate { get; set; }

        public string Status { get; set; }
    }

which I am using to return the results of the query:

我用来返回查询结果:

    public IQueryable<StatusList> GetLocationStatus()
    {
        var status = (from location in this.ObjectContext.Locations
                      where location.InspectionReadings.Status == value
                      orderby a.DateTaken                          
                      select new LocationStatusList()
                      {
                          ID = a.ID,
                          LocationName = d.Name,                              
                      }).ToList<StatusList>();
        return status;              
    }

unfortunately, it's returning the error in the title and I have no idea why as the list is clearly a list item and i have converted the results

不幸的是,它返回了标题中的错误,我不知道为什么,因为列表显然是一个列表项,我已经转换了结果

.ToList<LocationStatusList>

采纳答案by Jon Skeet

The problem is precisely becauseyou've called ToList(). You've declared that you're returning IQueryable<LocationStatusList>, and List<T>doesn't implement IQueryable<T>.

问题正是因为您调用了ToList(). 您已经声明您要返回IQueryable<LocationStatusList>,并且List<T>没有实现IQueryable<T>.

Options (pick one):

选项(选择一项):

  • Remove the ToListcall
  • Change the return type to IEnumerable<LocationStatusList>, IList<LocationStatusList>or possibly List<LocationStatusList>
  • Call AsQueryable()after ToList():

    ... as before ...
    .ToList().AsQueryable();
    
  • 取消ToList通话
  • 将返回类型更改为IEnumerable<LocationStatusList>IList<LocationStatusList>或者可能List<LocationStatusList>
  • 调用AsQueryable()ToList()

    ... as before ...
    .ToList().AsQueryable();
    

Note that you don't need the type argument in the ToListcall - it's the same one that the compiler would infer anyway.

请注意,您不需要ToList调用中的类型参数- 它与编译器无论如何都会推断出的参数相同。