将具有in运算符的sql查询转换为linq

时间:2019-07-08 08:11:44

标签: c# linq

我有这个SQL查询:

select * from jobItem 
                        join taskItem on jobItem.JobItemId = taskItem.JobItemId
                        join taskWidgetCountItem on taskItem.TaskItemId = taskWidgetCountItem.TaskItemId
                        join EntityItem on EntityItem.EntityItemId = TaskItem.EntityItemId
                        left join costCenterItem on taskItem.EntityItemId = costCenterItem.EntityItemId 
                        left join facilityItem on taskItem.FacilityItemId = facilityItem.FacilityItemId 
                        where  
                              TaskWidgetCountItem.WidgetItemId in ( select WidgetItemId from BillingInformationItem)

我想将此查询转换为linq

1 个答案:

答案 0 :(得分:0)

与LINQ中的SQL IN子句等效的是Contains语句。

IEnumerable<long> validWigetItemIds; // fill from Database or input parameters

var result = DbContext.JobItems
    .Where(ji => validWigetItemIds.Contains(ji.WidgetItemId)); // equivalent to IN clause
    // other LINQ statements as required by your usecase