如何将 Linq 用于多列

本文关键字:用于 Linq | 更新日期: 2023-09-27 18:35:31

if (i == 1) //if dropdown #1 index > 0
{
    strSelectedType = ViewState["EN"].ToString();
    strSelectedCol = "Name";
}
if (i == 2) //if dropdown #2 index > 0
{
    strSelectedType = ViewState["SU"].ToString();
    strSelectedCol = "Super";
}
if (i == 3) //if dropdown #3 index > 0
{
    strSelectedType = ViewState["DT"].ToString();
    strSelectedCol = "Deran";
}
if (i == 4) //if dropdown #4 index > 0
{
    strSelectedType = ViewState["PR"].ToString();
    strSelectedCol = "PRate";
}
DataTable dtTest = new DataTable();
dtTest = (DataTable)ViewState["gvDataTable"];
DataTable selectedTable = dtTest.AsEnumerable()
              .Where(r => r.Field<string>(strSelectedCol) == strSelectedType)
              .CopyToDataTable();

上面的代码仅使用一列进行过滤。

如何修改dtText.AsEnumerable(),使其像这样过滤:

DataTable selectedTable = dtTest.AsEnumerable()
          .Where(if (dropdown #1 > 0) {r => r.Field<string>(strSelectedCol) == strSelectedType }
                if (dropdown #2 > 0) { r => r.Field<string>(strSelectedCol) == strSelectedType }
                if (dropdown #3 > 0) { r => r.Field<string>(strSelectedCol) == strSelectedType }
                if (dropdown #4 > 0) { r => r.Field<string>(strSelectedCol) == strSelectedType })
          .CopyToDataTable();

如果所选索引> 0,它将循环访问每个下拉追加筛选器。

如何将 Linq 用于多列

可以针对您的情况进行一些说明,例如先准备以下内容:

List<string> a = new List<string>() {"EN", "SU", "DT", "PR"};
List<string> b = new List<string>() {"Name", "Super", "Deran", "PRate"};

然后你可以简单地做:

strSelectedType = ViewState[a[i-1]].ToString();
strSelectedCol = b[i-1];    

然后,如果您有下拉列表列表,您也可以这样做

var query = dtTest.AsEnumerable();
for (int i = 0; i < 4; ++i)
  if (dropdown #i-1 index > 0) {
    query = query.Where(r => r.Field<string>(strSelectedCol) == ViewState[strSelectedType].ToString());
    var result = query.CopyToDataTable();
    //Do something, your DataTable is here
  }

您也可以使用List<KeyValuePair>

List<KeyValuePair<string,string>> lkvp = new List<KeyValuePair<string,string>>(){
    new KeyValuePair("EN", "Name"),
    new KeyValuePair("SU", "Super"),
    new KeyValuePair("DT", "Deran"),
    new KeyValuePair("PR", "PRate")
}

并像这样使用它

strSelectedType = ViewState[lkvp[i-1].Key].ToString();
strSelectedCol = lkvp[i-1].Value;    

使用谓词生成器。

特别是当您不知道要过滤的确切选项时,它会有所帮助。您只需在进入筛选点之前构建谓词。

它提高了应用程序的性能,因为无需多次筛选集合。

阅读以下链接:http://www.albahari.com/nutshell/predicatebuilder.aspx

您可以逐步构建查询。只需有条件地添加新过滤器并将结果分配给原始查询变量:

var query = dtTest.AsEnumerable();
if (dropdown1.SelectedIndex > 0)
    query = query.Where(r => r.Field<string>("Name") == ViewState["EN"].ToString());
if (dropdown2.SelectedIndex > 0)
    query = query.Where(r => r.Field<string>("Super") == ViewState["SU"].ToString());
// etc
DataTable selectedTable = query.CopyToDataTable();

如果您可以更改 ViewState 键,以便它们与下拉列表 ID 匹配(反之亦然),那么您可以轻松地构建过滤器列表,然后在循环中应用它们:

var columns = new Dictionary<string, string> {
    ["EN"] = "Name",
    ["SU"] = "Super",
    ["DT"] = "Deran",
    ["PR"] = "PRate"
};
var filters = from ddl in Controls.OfType<DropDownList>()
              where ddl.SelectedIndex > 0 && columns.ContainsKey(ddl.ID)
              select new {
                  Column = columns[ddl.ID], 
                  Value = ViewState[ddl.ID].ToString()
              };
var query = dtTest.AsEnumerable();
foreach(var fitler in filters)
   query = query.Where(r => r.Field<string>(fitler.Column) == fitler.Value);
DataTable selectedTable = query.CopyToDataTable();

Where调用可以链接:

var rows = dtTest.AsEnumerable();
if (...)
    rows = rows.Where(r => ...);
if (...)
    rows = rows.Where(r => ...);
...
var result = rows.CopyToDataTable();

或者,如果要存储条件以供以后使用,也可以这样做:

var conditions = new List<Func<DataRow, bool>>();
if (...)
    conditions.Add(r => ...);
if (...)
    conditions.Add(r => ...);
...
var query = dtTest.AsEnumerable();
foreach (var cond in conditions)
    query = query.Where(cond);
var result = query.CopyToDataTable();