tags:

views:

73

answers:

1

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!

+3  A: 

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);
womp
What if I don't want to code a class? Can I return as some sort of anonymous or untyped collection?
Nope.. the whole purpose of the var keyword is to let you deal with anonymous types conveniently, but it also means that your types are generated at runtime. If you're really needing to propagate a bunch of anonymous types around, you might be doing something wrong in the overall design. If you're not working with the retrieved data locally, you must be adding it to something or storing it somewhere, and you don't want to use anonymous types for that kind of functionality.
womp
@womp "....you might be doing something wrong in the overall design" <-- you might be right! :)Thanks!