无法隐式转换类型'System.Linq.IQueryable'到'System.Collections.Generic.IList“类型、隐式、System、Linq

2023-09-03 10:06:00 作者:酒醉出人性

我有一个方法:

public DzieckoAndOpiekunCollection GetChildAndOpiekunByFirstnameLastname(string firstname, string lastname)
{
    DataTransfer.ChargeInSchoolEntities db = new DataTransfer.ChargeInSchoolEntities();
    DzieckoAndOpiekunCollection result = new DzieckoAndOpiekunCollection();
    if (firstname == null && lastname != null)
    {
        IList<DzieckoAndOpiekun> resultV = from p in db.Dziecko
                      where lastname == p.Nazwisko
                      **select** new DzieckoAndOpiekun(
                     p.Imie,
                     p.Nazwisko,
                     p.Opiekun.Imie,
                     p.Opiekun.Nazwisko)
                  ;
        result.AddRange(resultV);
    }
    return result;
}

和在选定的地点错误:

错误1无法隐式转换类型'System.Linq.IQueryable'到'System.Collections.Generic.IList。一个显式转换存在(是否缺少强制转换?)

Error 1 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IList'. An explicit conversion exists (are you missing a cast?)

不知道如何解决我的问题?

Any idea how solve my problem?

推荐答案

要转换 IQuerable 的IEnumerable 到列表中,您可以执行下列操作之一:

To convert IQuerable or IEnumerable to a list, you can do one of the following:

IQueryable<object> q = ...;
List<object> l = q.ToList();

IQueryable<object> q = ...;
List<object> l = new List<object>(q);