Melanie
Melanie

Reputation:

Return Datatype of Linq Query Result

I think I'm missing something really basic.

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 have no idea how to return the results as a public method of a class.

Examples always seem to show returning the whole entity (like IQueryable<People>), but in this case I only want to return a subset as SomeKindOfList<int, string, string>. Since I can't use var, what do I use?

Thanks!

Upvotes: 4

Views: 3877

Answers (1)

womp
womp

Reputation: 116977

You can get concrete types out of a linq query, but in your case you are constructing anonymous types by using

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

If instead, you coded a class called "Person", and did this:

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

Your linq result (signatures) can be of type IEnumerable<Person>, and that 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);

Upvotes: 5

Related Questions