0

如果我想搜索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 * FROM .... WHERE ..... + condition 

如何使用与 LINQ TO SQL 实体相同的概念进行搜索?

4

1 回答 1

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();
于 2013-06-13T14:14:41.393 回答