Hello Guys,
I was wondering if i extract the common where clause query into a common expression would it make my query much faster, if i have say something like 10 linq queries on a collection with exact same 1st part of the where clause.
I have done a small example to explain a bit more .
public class Person
{
public string First { get; set; }
public string Last { get; set; }
public int Age { get; set; }
public String Born { get; set; }
public string Living { get; set; }
}
public sealed class PersonDetails : List<Person>
{
}
PersonDetails d = new PersonDetails();
d.Add(new Person() {Age = 29, Born = "Timbuk Tu", First = "Joe", Last = "Bloggs", Living = "London"});
d.Add(new Person() { Age = 29, Born = "Timbuk Tu", First = "Foo", Last = "Bar", Living = "NewYork" });
Expression<Func<Person, bool>> exp = (a) => a.Age == 29;
Func<Person, bool> commonQuery = exp.Compile();
var lx = from y in d where commonQuery.Invoke(y) && y.Living == "London" select y;
var bx = from y in d where y.Age == 29 && y.Living == "NewYork" select y;
Console.WriteLine("All Details {0}, {1}, {2}, {3}, {4}", lx.Single().Age, lx.Single().First , lx.Single().Last, lx.Single().Living, lx.Single().Born );
Console.WriteLine("All Details {0}, {1}, {2}, {3}, {4}", bx.Single().Age, bx.Single().First, bx.Single().Last, bx.Single().Living, bx.Single().Born);
So can some of the guru's here give me some advice if it would be a good practice to write query like
var lx = "Linq Expression "
or
var bx = "Linq Expression" ?
Any inputs would be highly appreciated.
Thanks,
AG