views:

135

answers:

1

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

+4  A: 

Firstly, Eric's absolutely right: if you're concerned about performance, you need to measure it. Work out exactly what you want to measure, and record what happens for each change you make in your code. There are various aspects of benchmarking which I don't have time to go into now, but the key one is probably to make sure you run the tests for long enough for them to be meaningful - if your test only takes 50ms, you're unlikely to be able to tell improvements in your code from noise.

Now, if you're using LINQ to Objects then you almost certainly don't want to be using expression trees at all. Stick to delegates - that's what LINQ to Objects uses anyway.

Now, as for restructuring... if you've got a common predicate, then you can filter your list by that to start with, to come up with a new IEnumerable<T>. The predicate will be applied lazily, so it won't make any difference to execution speed, but it might make your code more readable. It could slow things down very slightly as it will introduce an extra level of indirection when you've effectively got two different where clauses.

If the result of applying the filters will have very few results, you may want to materialize it (e.g. by calling ToList) and remember the result - that way you don't need to query the whole thing again for the second query.

However, the big benefit that I can see would be from only calling Single once for each query. Currently you're executing the whole query for every single property - that's clearly inefficient.

Here's your code, rewritten accordingly - and using a collection initializer too:

PersonDetails d = new PersonDetails
{
    new Person {Age = 29, Born = "Timbuk Tu", First = "Joe", 
               Last = "Bloggs", Living = "London"},
    new Person { Age = 29, Born = "Timbuk Tu", First = "Foo", 
                Last = "Bar", Living = "NewYork" }
};

var peopleOfCorrectAge = d.Where(a => a.Age == 29);

var londoners = peopleOfCorrectAge.Where(p => p.Living == "London");
var newYorkers = peopleOfCorrectAge.Where(p => p.Living == "New York");

var londoner = londoners.Single();
var newYorker = newYorker.Single();

Console.WriteLine("All Details {0}, {1}, {2}, {3}, {4}",
                  londoner.Age, londoner.First, 
                  londoner.Last, londoner.Living, londoner.Born);

Console.WriteLine("All Details {0}, {1}, {2}, {3}, {4}",
                  newYorker.Age, newYorker.First, 
                  newYorker.Last, newYorker.Living, newYorker.Born);

Alternatively, for the last section, encapsulate the "writing out a single person":

DisplayPerson(londoners.Single());
DisplayPerson(newYorkers.Single());

...

private static void DisplayPerson(Person person)
{
    Console.WriteLine("All Details {0}, {1}, {2}, {3}, {4}",
                      person.Age, person.First, 
                      person.Last, person.Living, person.Born);
}
Jon Skeet
Thanks @Jon, Yeah "If the result of applying the filters will have very few results, you may want to materialize it (e.g. by calling ToList) and remember the result " makes a lot of sense and yeah certainly calling single every time is not that good. Point noted.
netmatrix01