【问题标题】:Optional Parameters in EntityFrameWork queryEntityFrameWork 查询中的可选参数
【发布时间】:2015-09-14 13:44:45
【问题描述】:

我的方法签名如下。只有 Id 是强制性的,所有其他三个参数都是可选的。我需要在 where 子句中处理这个问题。如果存在可选参数,那么我需要将它们添加到 where 子句中,否则查询应该基于 Id 以及可选参数中可用的任何内容

其次,我们可以使用 rowVersion 进行比较,如下面的查询中指定的那样。 (Sql Server中的rowVersion是时间戳)

GetRecords(int  Id, int[] LocationId = null, int PayrollNo = 0, byte[] rowVersion=null)

 {

     var result = this.context.employees.where(x=>x.Id == id && LocationId.Contains(x.locationId) && x.payrollNo ==PayrollNo && x.rowVersion >  rowVersion);

}

任何帮助将不胜感激。

【问题讨论】:

    标签: c#-4.0 entity-framework-6 optional-parameters


    【解决方案1】:

    试试这个:

    GetRecords(int  Id, int[] LocationId = null, int PayrollNo = 0, byte[] rowVersion=null)
    
     {
    
         var result = this.context.employees.where(x=>x.Id == id);
         if (LocationId != null)
             result = result.Where(x=>LocationId.Contains(x.locationId));
         if (PayrollNo > 0)
             result = result.Where(x=>x.payrollNo == PayrollNo);
         if (rowVersion != null)
             result = result.Where(x=>x.rowVersion >  rowVersion);
    
    }
    

    【讨论】:

    • 我们可以这样比较 RowVersion 吗?我的实体将 RowVersion 表示为 byte[ ] 而它是 SQL Server 中的时间戳
    • 看起来您可能需要一种解决方法。 stackoverflow.com/questions/17761114/…
    猜你喜欢
    • 1970-01-01
    • 2023-02-16
    • 2020-09-08
    • 2020-12-22
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 2013-05-13
    • 1970-01-01
    相关资源
    最近更新 更多