2

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

mit
  • 1,763
  • 4
  • 16
  • 27
Serdar Şengül
  • 77
  • 1
  • 1
  • 6

4 Answers4

3

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;
    }
spajce
  • 7,044
  • 5
  • 29
  • 44
1

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; }
har07
  • 88,338
  • 12
  • 84
  • 137
  • the same error message?? if it is different please post the latest error message – har07 Feb 06 '14 at 08:09
  • yes the same error message.. personItems.Items = query.ToList(); Error 1 Cannot implicitly convert type System.Collections.Generic.List' to System.Collections.Generic.List' C:\TFS\Projects\Ship\PersonData\Persons.cs 50 31 PersonData – Serdar Şengül Feb 06 '14 at 08:16
  • check my updated answer. Items property is of different type from what linq query returns, hence you got that conversion error – har07 Feb 06 '14 at 08:26
0

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.

Siva Charan
  • 17,940
  • 9
  • 60
  • 95
  • so yes I already tried it...but the same mistake again List crewItems = new List(); and I don't need that,because they already have a List class – Serdar Şengül Feb 06 '14 at 08:09
0

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());
}
tom.maruska
  • 1,411
  • 16
  • 22