How to convert LINQ query result to list and return generic List?

时光总嘲笑我的痴心妄想 提交于 2019-12-07 18:39:01

问题


I used generic ORM list example.

This is my table.

Person table

Guid
Name
LastName

and this is my struct class.

 public struct PersonItem   // database and class field names are the same
    {
        public Guid   Guid{ get; set; } 
        public string Name { get; set; }
        public string LastName { get; set; }
    }


  public struct PersonItems
    {
        public PersonItems()
        {
            Items = new List<PersonItem>();
        }

        public List<PersonItem> Items { get; set; }
    }

I'm using such and no problem but I always have to write field's

public PersonItems GetPersons()
        {
            var query = (from p in _DbEntities.t_Crew
                         select p).ToList();

            if (query != null)
            {
                foreach (var item in query)
                {
                    _PersonItems.Items.Add(new PersonItem
                        {
                            Guid = item.Guid,
                            Name = item.Name,
                            LastName = item.LastName

                        });
                }
            }

            return _PersonItems;

        }   


   public PersonItems GetPersons()
    {
        PersonItems personItems = new PersonItems();

        var query = from p in _DbEntities.t_Person
                    select p; >> this here query I need to convert linq query result to list

       personItems = query.ToList();
       return personItems ;
    }

error

Cannot implicitly convert type System.Collections.Generic.List' to PersonData.PersonItems'.


回答1:


Try this

 public PersonItems GetPersons()
    {
        PersonItems personItems = new PersonItems();

        var query = (from p in _DbEntities.t_Person
                    select new PersonItems 
                    {
                        test = p.SomeName,
                        //Other Stuff
                        //...
                    }).ToList();
       return query;
    }



回答2:


Comparing to the other version of your GetPersons() method, I think this line :

personItems = query.ToList();

should've been this way instead :

personItems.Items = query.ToList();

Update regarding the latest error. You can't assign list of t_Person to Item property which is of type list of PersonItem. Hence, your query need to be adjusted to return list of PersonItem :

var query = from p in _DbEntities.t_Person
            select new PersonItem
                        {
                            Guid = p.Guid,
                            Name = p.Name,
                            LastName = p.LastName
                        };

or other option is to change definition of Item property to list of t_Person :

public List<t_Person> Items { get; set; }



回答3:


Cannot implicitly convert type System.Collections.Generic.List' to PersonData.PersonItems'

Above error says that you are trying to convert generic list to PersonItems.

So at this line, query.ToList() code returns List<PersonItem> not the PersonItems

    var query = from p in _DbEntities.t_Person
                select p; >> this here query I need to convert linq query result to list

   personItems = query.ToList();
   return personItems ;

So thats the reason above line of code fails.




回答4:


What about this? Change .ctor of PersonItems:

public struct PersonItems
{
    public PersonItems(List<PersonItem> items)
    {
        Items = items;
    }

    public List<PersonItem> Items { get; set; }
 }

And then method GetPersons():

public PersonItems GetPersons()
{    
    return new PersonItems(_DbEntities.t_Person.ToList());
}


来源:https://stackoverflow.com/questions/21597097/how-to-convert-linq-query-result-to-list-and-return-generic-list

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