Cannot implicitly convert type 'System.Linq.IQ

2020-02-08 06:52发布

I have a method:

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;
}

and error in selected place :

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

Any idea how solve my problem?

6条回答
该账号已被封号
2楼-- · 2020-02-08 07:32

If using a where clause be sure to include .First() if you do not want a IQueryable object.

查看更多
萌系小妹纸
3楼-- · 2020-02-08 07:39

I've got solved my problem this way in EF on list of records from db

ListOfObjectToBeOrder.OrderBy(x => x.columnName).ToList();
查看更多
手持菜刀,她持情操
4楼-- · 2020-02-08 07:42

You can replace IList<DzieckoAndOpiekun> resultV with var resultV.

查看更多
家丑人穷心不美
5楼-- · 2020-02-08 07:45

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

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

or:

IQueryable<object> q = ...;
List<object> l = new List<object>(q);
查看更多
老娘就宠你
6楼-- · 2020-02-08 07:47

You can use the .ToList() method to convert the IQueryable result returned to an IList, as shown below, after the linq query.

   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).ToList()
                  ;
        result.AddRange(resultV);
    }
    return result;
}
查看更多
甜甜的少女心
7楼-- · 2020-02-08 07:54

Try this -->

 new DzieckoAndOpiekun(
                         p.Imie,
                         p.Nazwisko,
                         p.Opiekun.Imie,
                         p.Opiekun.Nazwisko).ToList()
查看更多
登录 后发表回答