C# 基于条件的Linq查询

C# 基于条件的Linq查询,c#,performance,entity-framework,linq,c#-4.0,C#,Performance,Entity Framework,Linq,C# 4.0,我必须检查col1、col2和col3值并选择正确的linq查询 这就是我使用if条件的方式 public DataTable GetRawData(string col1, string col2, string col3) { IQueryable<IF_Table> result = null; DataTable dt = new DataTable(); dt.Columns.Add(

我必须检查col1、col2和col3值并选择正确的linq查询

这就是我使用if条件的方式

 public DataTable GetRawData(string col1, string col2, string col3)
        {
            IQueryable<IF_Table> result = null;
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("DetailId");
            dt.Columns.Add("Description");
            DataRow row = null;
            if (!string.IsNullOrEmpty(col1))
            {
                 result = db.IF_Table.Where(x => x.col1 == col1);
            }
            if (!string.IsNullOrEmpty(col2))
            {
                result = db.IF_Table.Where(x => x.col2 == col2);
            }
            if (!string.IsNullOrEmpty(col3))
            {
                result = db.IF_Table.Where(x => x.col3 == col3);
            }
            if (!string.IsNullOrEmpty(col1) && !string.IsNullOrEmpty(col2))
            {
                result = db.IF_Table.Where(x => x.col1 == col1 && x.col2==col2);
            }
            if (!string.IsNullOrEmpty(col1) && !string.IsNullOrEmpty(col3))
            {
                result = db.IF_Table.Where(x => x.col1 == col1 && x.col3 == col3);
            }
            if (!string.IsNullOrEmpty(col2) && !string.IsNullOrEmpty(col3))
            {
                result = db.IF_Table.Where(x => x.col2 == col2 && x.col3 == col3);
            }
            if (!string.IsNullOrEmpty(col1) && !string.IsNullOrEmpty(col2) && !string.IsNullOrEmpty(col3))
            {
                result = db.IF_Table.Where(x =>x.col1==col1 && x.col2 == col2 && x.col3 == col3);
            }
            foreach ( var rowObj in result)
            {
                row = dt.NewRow();
                dt.Rows.Add(rowObj.Id, rowObj.DetailId,rowObj.Description);
            }
            return dt;
        }
公共数据表GetRawData(字符串col1、字符串col2、字符串col3) { IQueryable结果=null; DataTable dt=新的DataTable(); dt.列。添加(“Id”); dt.列。添加(“详细ID”); dt.列。添加(“说明”); DataRow行=null; 如果(!string.IsNullOrEmpty(col1)) { 结果=db.IF_Table.Where(x=>x.col1==col1); } 如果(!string.IsNullOrEmpty(col2)) { 结果=db.IF_Table.Where(x=>x.col2==col2); } 如果(!string.IsNullOrEmpty(col3)) { 结果=db.IF_Table.Where(x=>x.col3==col3); } 如果(!string.IsNullOrEmpty(col1)和&!string.IsNullOrEmpty(col2)) { 结果=db.IF_Table.Where(x=>x.col1==col1&&x.col2==col2); } 如果(!string.IsNullOrEmpty(col1)和&!string.IsNullOrEmpty(col3)) { 结果=db.IF_Table.Where(x=>x.col1==col1&&x.col3==col3); } 如果(!string.IsNullOrEmpty(col2)和&!string.IsNullOrEmpty(col3)) { 结果=db.IF_Table.Where(x=>x.col2==col2&&x.col3==col3); } 如果(!string.IsNullOrEmpty(col1)和&!string.IsNullOrEmpty(col2)和&!string.IsNullOrEmpty(col3)) { 结果=db.IF_Table.Where(x=>x.col1==col1&&x.col2==col2&&x.col3==col3); } foreach(结果中的var rowObj) { row=dt.NewRow(); dt.Rows.Add(rowObj.Id、rowObj.DetailId、rowObj.Description); } 返回dt; }
有没有其他方法可以避免使用太多的if条件?任何帮助或建议都将不胜感激

定义基本查询,该查询将返回
db.IF\u表
。然后围绕这个结果构建查询。结果中的3
if
应涵盖您的所有案例

        IQueryable<IF_Table> result = db.IF_Table;
        if (!string.IsNullOrEmpty(col1))
        {
             result = result.Where(x => x.col1 == col1);
        }
        if (!string.IsNullOrEmpty(col2))
        {
            result = result.Where(x => x.col2 == col2);
        }
        if (!string.IsNullOrEmpty(col3))
        {
            result = result.Where(x => x.col3 == col3);
        }
IQueryable结果=db.IF\u表格;
如果(!string.IsNullOrEmpty(col1))
{
结果=结果,其中(x=>x.col1==col1);
}
如果(!string.IsNullOrEmpty(col2))
{
结果=结果,其中(x=>x.col2==col2);
}
如果(!string.IsNullOrEmpty(col3))
{
结果=结果,其中(x=>x.col3==col3);
}
如果希望在所有col1、col2、col3均为空字符串或null时返回空的
DataTable
。您可以在if语句中定义bool变量
fillTable
,并将其设置为
true
。在ifs之后,您可以检查
if(!fillTable)返回dt

您需要的。