Returns the Linq Query Result Data Type

I think I am missing something really elementary.

var signatures=from person in db.People where person.Active==true select new{person.ID, person.Lname, person.Fname}; 

This linq query works, but I donโ€™t know how to return the results as a public method of the class.

It always seems to examples that they return the whole object (for example, IQueryable <People >), but in this case I only want to return a subset as SomeKindOfList <int, string, string >. Since I cannot use var, what am I using?

Thanks!

+4
source share
1 answer

You can get specific types from a linq query, but in your case, you create anonymous types using

 select new{person.ID, person.Lname, person.Fname}; 

If instead you encoded a class called "Man" and did the following:

 select new Person(peson.ID, person.Lname, person.Fname); 

Your linq (signature) result may be of type IEnumerable<Person> , and it is a type that you can return from your function.

Example:

  IEnumerable<Person> signatures = from person in db.People where person.Active==true select new Person(person.ID, person.Lname, person.Fname); 
+5
source

Source: https://habr.com/ru/post/1286155/


All Articles