Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refactor select part of Linq expression?

I'm playing around with some Linq-SQL stuff, doing something like this:

var foo = from f in db.Foo where f.Bar > 5 select f;

which is all fine and dandy, and I know I can also do this:

var foo = from f in db.Foo where f.Bar > 5 select new { f.Bar, f.Baz };

What I want to know, is can I factor out the select part of that query, if I want to determine at runtime what parts of Foo to select? Such as:

var foo = from f in db.Foo where f.Bar > 5 select SomeMethodThatReturnsThePropertiesOfFooIReallyWant();

Edit to clarify: I'm looking for the syntax and return type of SomeMethod...().

If I wanted to do this some times:

select new { f.Bar, f.Baz };

but other times do this:

select new { f.Baz, f.Other };

Based on data in memory (without doing a giant case statement), how would i do that, if possible?

like image 957
Jonas Avatar asked Nov 06 '22 22:11

Jonas


1 Answers

Sure, although it's easier in the fluent syntax:

var query_foo = db.Foo.Where(f=>f.Bar >  5);
//  :
var foo =query_foo.Select(f=>SomeMethodThatReturnsEtc(f));
like image 157
James Curran Avatar answered Nov 15 '22 06:11

James Curran