none
LINQ query using where and group by RRS feed

  • Question

  • Hi,

    I have a List Collection in which i fetched all possible records,now i query in that List collection using LINQ in my aspx page,

    But i have to make some conditions in my query.

    <%
    foreach (var item in Leads.GroupBy(x => new { x.Center.CenterId, x.Agent.AgentId, x.Client.ClientId }))
    {
    totalBillableLeads = 0;
    var Lead = Leads.FirstOrDefault(x => x.Agent.AgentId == item.Key.AgentId);
    Client clients = Leads.FirstOrDefault(x => x.Client.ClientId == item.Key.ClientId).Client;
    Center center = Leads.FirstOrDefault(x => x.Center.CenterId == item.Key.CenterId).Center;
    Agent agent = Leads.FirstOrDefault(x => x.Agent.AgentId == item.Key.AgentId).Agent;
    %>

    }

    In above code Leads is List Collection....in that i want to apply where clause like this

    foreach (var item in Leads.GroupBy(x => new { x.Center.CenterId, x.Agent.AgentId, x.Client.ClientId }).Where(status='2'))
    {

     //mycode

    }

    but i am unable to do that. Basically in my List there is a column name Status and i want to check that status while fetching the records.

    Please Help.

    Many thanks in advance.

    Regards,

    Mithran.

    Best Regards, Mithran.

    • Moved by 许阳(无锡) Monday, April 23, 2012 8:43 AM Data Issue (From:Visual Studio Tools for Office)
    Sunday, April 22, 2012 9:58 PM

Answers

  • Hi MithranM,

    Welcome to MSDN Forum.

    I suggest you to filter the records before executing foreach statement. the query statement in a foreach will not hit the databae and be executed. You can do the filtering before foreach and load the records into memory. For eaxample,

    var list = Leads.GroupBy(x=> new {x.Center.CenterId, x.Agent.AgentId, x.Client.ClientId}).Where(status=="2");
    
    foreach(var item in list)
    {
        ...
    }

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, April 24, 2012 2:31 AM
    Moderator

All replies