如何在 C# linq 中添加 sql 'when' 功能

How to add sql 'when' functionality in c# linq

我有以下参数:

public object GetDataByProjectCostID(string employeeid, DateTime costdate, int id = 0)

以及查询:

var projectCost = (from pc in db.ProjectCosts
                           where pc.ProjectCostID == id
                           where System.Data.Entity.DbFunctions.TruncateTime(pc.CostDate) == costdate.Date
                          
                           join p in db.Projects
                           on pc.ProjectID equals p.ProjectID

                           join sct in db.SubCostTypes
                           on pc.SubCostTypeID equals sct.SubCostTypeID

                           join ct in db.CostTypes
                           on sct.CostTypeID equals ct.CostTypeID

                           select new
                           {
                               p.ProjectName,
                               ct.CostTypeName,
                               ct.CostTypeID,
                               sct.SubCostTypeName,
                               pc.ProjectID,
                               pc.ProjectCostID,
                               pc.SubCostTypeID,
                               pc.Amount,
                               pc.Quantity,
                               pc.Note,
                               pc.CreatedBy,
                               sct.Unit,
                               pc.CreateDate,
                               pc.CostDate,
                               pc.ProjectCostImage


                           }).ToList();

现在我的问题是如何在查询中添加可选参数。 假设如果请求中不存在 id 那么我需要跳过 where 子句

where pc.ProjectCostID == id

在 sql 中我们可以使用 when 子句,但在 LINQ 中有什么用呢? 提前致谢。

试试这个

 where (( id==0 || pc.ProjectCostID == id)
 && System.Data.Entity.DbFunctions.TruncateTime(pc.CostDate) == costdate.Date)

我在 LinQ fluent form 中使用了这个技巧,你可以试试这个:

//Make id nullable
public object GetDataByProjectCostID(string employeeid, DateTime costdate, int? id)
{
var projectCost = (from pc in db.ProjectCosts
                               //This expression is evaluated only if id has a value
                               where (!id.HasValue || pc.ProjectCostID == id)
                               && System.Data.Entity.DbFunctions.TruncateTime(pc.CostDate) == costdate.Date

                               join p in db.Projects
                               on pc.ProjectID equals p.ProjectID

                               join sct in db.SubCostTypes
                               on pc.SubCostTypeID equals sct.SubCostTypeID

                               join ct in db.CostTypes
                               on sct.CostTypeID equals ct.CostTypeID

                               select new
                               {
                                   p.ProjectName,
                                   ct.CostTypeName,
                                   ct.CostTypeID,
                                   sct.SubCostTypeName,
                                   pc.ProjectID,
                                   pc.ProjectCostID,
                                   pc.SubCostTypeID,
                                   pc.Amount,
                                   pc.Quantity,
                                   pc.Note,
                                   pc.CreatedBy,
                                   sct.Unit,
                                   pc.CreateDate,
                                   pc.CostDate,
                                   pc.ProjectCostImage


                               }).ToList();
}