简体   繁体   中英

Translate SQL to LINQ query - group/join/filter

I have the following query:

SELECT S.[FlowOrder], S.[DESCRIPTION], COUNT(I.ID)
FROM WorkFlowStatus AS S
INNER JOIN Item AS I
    ON S.ID = I.StatusID
WHERE I.Installation = '1'
GROUP BY S.[Description], S.[FlowOrder]
ORDER BY S.[FlowOrder]

Which gives me the count of an item, grouped by a foreign key to workflow, outputting the descriptive name from my FK table.

I've go this far with the LINQ query (using LINQ-to-SQL) in the background:

var items = from s in _entities.WorkflowStatus
    join i in _entities.Items on s.ID equals i.StatusId
    into Statuses
    orderby s.FlowOrder
    select new {s.Description, ItemCount = Statuses.Count() };

How do I get the where clause in the SQL into this LINQ query?

How about this?

var items = from s in _entities.WorkflowStatus
    join i in _entities.Items.Where(item=>item. Installation == "1") on s.ID equals i.StatusId
    into Statuses
    orderby s.FlowOrder
    select new {s.Description, ItemCount = Statuses.Count() };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM