2015-06-27 34 views
0

我的方法签名如下。只有Id是强制性的,所有其他三个参数都是可选的。我需要在where子句中处理这个问题。如果存在可选项,那么我需要将它们添加到where子句中,否则查询应该建立在Id上,并且可以从可选参数中获得。EntityFrameWork查询中的可选参数

其次,我们可以在下面的查询中指定的比较中使用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); 

} 

任何帮助将不胜感激。

回答

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); 

} 
+0

我们可以比较RowVersion这样吗?我的实体将RowVersion表示为byte [],而它是SQL Server中的时间戳 – InTheWorldOfCodingApplications

+0

看起来您可能需要一种解决方法。 http://stackoverflow.com/questions/17761114/select-new-records-by-timestamp-column-with-entity-framework –