Linq to entities query for donor registrations I'm using Linq-to-Entities for querying, and MVC.NET and C# for coding. I'm trying to generate monthly donor registry report which needs donor count, donors who signed through web,donors who registered in current month and all this is to be display per county.

private MonthlySummaryViewModel
BuildMonthlySummaryViewModel(MonthlySummarySubModel model){

// Get the bounding datetimes for the previous and lookup month

DateTime curr = new DateTime(model.Year, model.Month, 1),                  
prev = curr.AddMonths(-1),    
next = curr.AddMonths(1);      
var monthlySummary = (from county in context.Counties.AsEnumerable()    
                      let query = context.DonorStatusActions.Where(x =&gt; x.Donor.CountyId == county.CountyId &amp;&amp; x.ActionTakenOn &gt;= prev &amp;&amp; x.ActionTakenOn &lt; next)

let data = query.GroupBy(x =&gt; x.Donor, x =&gt; x, (key, grp) =&gt; new {Item1 = key, Item2 = grp.OrderByDescending(y =&gt; y.ActionTakenOn).FirstOrDefault()})                                 
                                  .Where(x =&gt; x.Item2.IsDonor &amp;&amp; ((model.WebSignUp != "2" &amp;&amp; x.Item2.Donor.WebSignUp == (model.WebSignUp == "1" ? true : false)) || model.WebSignUp == "2"))  
                                  let registrations = data.Select(x =&gt; x.Item2)   
                                  let previous = registrations.Count(x =&gt; x.ActionTakenOn &gt;= prev &amp;&amp; x.ActionTakenOn &lt; curr)                                                                                let current = registrations.Count(x =&gt; x.ActionTakenOn &gt;= curr &amp;&amp; x.ActionTakenOn &lt; next)    
select Tuple.Create(county.Name, previous, current)).ToList();

 return new MonthlySummaryViewModel()
 {   
 Month = new System.Globalization.DateTimeFormatInfo().GetMonthName(model.Month),  
Year = model.Year,  
WebSignUp=DropDownLists.SignUpMethodList.First(m =&gt; m.Value==model.WebSignUp).Text,CountyData = monthlySummary,  
TotalPrevious = monthlySummary.Sum(x =&gt; x.Item2),  
TotalReport = monthlySummary.Sum(x =&gt; x.Item3), };}
First thing to do, is to layout the query so that you can actually read it. Optimizing something you can't read is not really possible.

var monthlySummary = 
    (from county in context.Counties.AsEnumerable()
     let query = context.DonorStatusActions
                        .Where(x =&gt; x.Donor.CountyId == county.CountyId 
                                 &amp;&amp; x.ActionTakenOn &gt;= prev 
                                 &amp;&amp; x.ActionTakenOn &lt; next)
     let data = query
         .GroupBy(x =&gt; x.Donor, x =&gt; x, (key, grp) =&gt; new 
             { 
                 Item1 = key, 
                 Item2 = grp.OrderByDescending(y =&gt; y.ActionTakenOn)
                            .FirstOrDefault()
             })
         .Where(x =&gt; x.Item2.IsDonor 
                 &amp;&amp; ((model.WebSignUp != "2" 
                     &amp;&amp; x.Item2.Donor.WebSignUp == (model.WebSignUp == "1" ? true : false)) 
                 || model.WebSignUp == "2"))
     let registrations = data.Select(x =&gt; x.Item2)   
     let previous = registrations.Count(x =&gt; 
         x.ActionTakenOn &gt;= prev &amp;&amp; x.ActionTakenOn &lt; curr)
     let current = registrations.Count(x =&gt; 
         x.ActionTakenOn &gt;= curr &amp;&amp; x.ActionTakenOn &lt; next)
     select Tuple.Create(county.Name, previous, current)
    ).ToList();


Yikes!

Ok let's remove the fluff.

from county in context.Counties.AsEnumerable()


Counties is already an Enumerable - it's an IQueryable&lt;County&gt;, and by soft-casting it like this, you've told LINQ "don't bother translating that to T-SQL, give me all counties and let LINQ-to-Objects deal with it instead" (see this answer).

If you ran a SQL profiler when your code runs, I bet you're not going to see the T-SQL you'd expect.

Just do this instead:

from county in context.Counties


This part needs a cleanup:

 .Where(x =&gt; x.Item2.IsDonor 
         &amp;&amp; ((model.WebSignUp != "2" 
             &amp;&amp; x.Item2.Donor.WebSignUp == (model.WebSignUp == "1" ? true : false)) 
         || model.WebSignUp == "2"))


I'm scratching my head over this one:

x.Item2.Donor.WebSignUp == (model.WebSignUp == "1" ? true : false)


Why not just do this instead?

x.Item2.Donor.WebSignUp == (model.WebSignUp == "1")


I have to say that Donor.WebSignUp being a bool while model.WebSignUp is a string, is pretty confusing.

These magic "1" and "2" values are annoying. They have a special meaning, but nobody knows what it is. Looks like a misused string that should be an int, or even better, an enum.

Regardless, the condition is redundant and could be simplified to this:

 .Where(x =&gt; x.Item2.IsDonor &amp;&amp; (
     model.WebSignUp == "2" || x.Item2.Donor.WebSignUp == (model.WebSignUp == "1")))


Be sure to profile the generated T-SQL and show execution plan to have SQL Server tell you if any index could speed things up; I suspect you might need one on Donors.WebSignUp, too.