score:2

The problem here isn't with what LINQ is actually doing. The query for scenario 2 would just create something similar to:

SELECT * FROM
PERSON
WHERE Department = N'Development'
AND Gender = N'Male'
AND Role = N'Manager'

You need to run this against the database directly to figure out what's your performance bottleneck. You're most likely missing indexes and therefore forcing your database server to scan the entire table to get the result set back.

If you're using SQL Server, try running a profiler to determine the exact query that (assuming) LINQ-to-EF is generating and tune that.

score:-2

You need to optimize your DB, take a look on index

You can also do the same request with Specification pattern

    public class NamedPersonSpecification : AbstractSpecification<Person>
{
    private string _name;
    public NamedPersonSpecification(string name)
    {
        this._name= name;
    }

    public override bool IsSatisfiedBy(Person o)
    {
        return o.Name.Equals(_name);
    }
}

And Query like this:

IQueryable<T>.Where(specification.IsSatisfiedBy)

The advantage is to be more clear to request your LINQ


Related Articles