c# - Where does the .ToList() go in LINQ query result -


when getting data populate data grid, have pulled data, made list , mapped model, this:

    public list<person> getpeople()     {         var data = _db.person.tolist();         list<person> people = new list<person>();          foreach (var item in data)         {             person person = new person()             {                 personid = item.personid,                 firstname = item.firstname,                 lastname = item.lastname             };             people.add(person);         }         return people;     } 

this works fine, in interest of writing less code discovered .select method , (what think is) anonymous type. have:

    public list<person> getpeople()     {         return _db.person.select(d => new person         {             personid = item.personid,             firstname = item.firstname,             lastname = item.lastname         }).tolist();     } 

though not easy read, works fine too. there preference either way?

the second preferred in cases. main differences are:

  1. the second example turn projection sql statement, returning columns ask for.
  2. the first example creates 2 lists, 1 of temporary , eligible garbage collection relatively quickly. may issue if people table large.

note if did not have tolist() after _db.person 2 methods virtually identical.

and not creating anonymous type since specify type (person). create anonymous type have new {... properties ...}

though not easy read,

i think once used linq statements select you'll find very easy read.


Comments

  1. Thanks, so fascinating! To read how you talked about the importance of skills in backend developer jobs. Choosing the right platform that gets you a good authentic work experience and showcases your skills in the right manner is very important. Eiliana.com is one of the best freelancing platforms you can connect with.

    ReplyDelete
  2. Agencies are also concentrating their efforts on world777 gaming, which is expected to be worth $300 billion by 2022. India accounts just a fragment of this, with a market share of about 1%. Given that Indian gambling companies spent Rs 1400 crore on advertising in 2021 alone, the figure is nothing to sneeze at.

    ReplyDelete

Post a Comment

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -