Optional Parameters in EntityFrameWork query

c#-4.0 entity-framework-6 optional-parameters

Question

My method signatures are as following. Only Id is mandatory, all other three parameters are option. I need to handle this in where clause. If optional ones are present then i need to add them to where clause otherwise query should be built on Id and whatever is available from Optional parameters

Secondly can we use rowVersion in comparison as specified in the query below. (rowVersion in Sql Server is a time stamp)

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

}

any help would be much appreciated.

1
0
6/27/2015 7:55:15 PM

Popular Answer

Try this:

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

}
1
6/28/2015 12:03:55 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow