如何使用Linq进行多列

时间:2016-02-19 13:59:45

标签: c# linq

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

如果所选索引&gt;它将遍历每个下拉附加过滤器。 0

4 个答案:

答案 0 :(得分:4)

可以对你的案例进行一些简化,例如首先做好准备:

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;    

答案 1 :(得分:3)

使用谓词构建器。

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

它可以改善您的应用程序执行情况,因为您不需要多次过滤您的收藏。

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

答案 2 :(得分:2)

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

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

答案 3 :(得分:1)

可以链接

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