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

11,988

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

Solution 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; }
Share:
11,988
Serdar Şengül
Author by

Serdar Şengül

Updated on June 04, 2022

Comments

  • Serdar Şengül
    Serdar Şengül almost 2 years

    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'.

  • har07
    har07 about 10 years
    the same error message?? if it is different please post the latest error message
  • Serdar Şengül
    Serdar Şengül about 10 years
    so yes I already tried it...but the same mistake again List<PersonItem> crewItems = new List<PersonItem>(); and I don't need that,because they already have a List class
  • Serdar Şengül
    Serdar Şengül about 10 years
    yes the same error message.. personItems.Items = query.ToList(); Error 1 Cannot implicitly convert type System.Collections.Generic.List<PersonData.t_Person>' to System.Collections.Generic.List<PersonData.Defination.Person‌​Item>' C:\TFS\Projec‌​ts\Ship\PersonData\P‌​ersons.cs 50 31 Pers‌​onData
  • har07
    har07 about 10 years
    check my updated answer. Items property is of different type from what linq query returns, hence you got that conversion error
  • Serdar Şengül
    Serdar Şengül about 10 years
    because sometimes I'm using wcf service and table join,I can not use your proposal