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

怎甘沉沦 提交于 2019-12-21 02:28:34

问题


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:

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:

    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>

回答1:


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

Options (pick one):

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

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

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



来源:https://stackoverflow.com/questions/10516086/cannot-implicitly-convert-type-system-collections-generic-listt-to-system-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!