如果我想按req_ser ,req_year .. ,so on fields
我执行以下操作:
if (obj.RequestSerial != 0)
{
if (condition != "")
condition = condition + " AND ";
condition += " req_ser= " + obj.RequestSerial;
}
if (obj.RequestYear != 0)
{
if (condition != "")
condition = condition + " AND ";
condition += " req_year = " + obj.RequestYear;
}
然后在我的select语句中执行以下操作:
SELECT * FROM .... WHERE ..... + condition
如何使用与LINQ TO SQL实体相同的概念进行搜索?
答案 0 :(得分:6)
你可以使用相同的逻辑......
var query = DB.MyItems;
if(conditionA != 0)
query = query.Where(x => x.A == conditionA);
if(conditionB != 0)
query = query.Where(x => x.B == conditionB);
var items = query.ToList();
使用问题中的属性,您的代码可能如下所示:
var query = DB.MyItems;//I don't know what you table it called
if(obj.RequestSerial != 0)
query = query.Where(x => x.req_ser == obj.RequestSerial);
if(obj.RequestYear != 0)
query = query.Where(x => x.req_year== obj.RequestYear);
var items = query.ToList();