简体   繁体   English

LINQ查询不返回任何行

[英]LINQ query returning no rows

I'm new to LINQ, so I'm sure there's an error in my logic below. 我是LINQ的新手,所以我确定下面的逻辑中有错误。

I have a list of objects: 我有一个对象列表:

class Characteristic
{
  public string Name { get; set; }
  public string Value { get; set; }
  public bool IsIncluded { get; set; }
}

Using each object in the list, I want to build a query in LINQ that starts with a DataTable , and filters it based on the object values, and yields a DataTable as the result. 使用列表中的每个对象,我想在LINQ中构建一个以DataTable开头的查询,并根据对象值对其进行过滤,并生成一个DataTable作为结果。

My Code so far: 到目前为止,我的代码:

DataTable table = MyTable;
// Also tried: DataTable table = MyTable.Clone();

foreach (Characteristic c in characteristics)
{
  if (c.IsIncluded)
  {
    var q = (from r in table.AsEnumerable()
             where r.Field<string>(c.Name) == c.Value
             select r);

    table = rows.CopyToDataTable();
  }
  else
  {
    var q = (from r in table.AsEnumerable()
             where r.Field<string>(c.Name) != c.Value
            select r);

    table = q.CopyToDataTable();
  }
}

UPDATE 更新

I was in a panicked hurry and I made a mistake; 我着急了,我犯了一个错误。 my DataTable was not empty, I just forgot to bind it to the DataGrid . 我的DataTable不为空,我只是忘记了将其绑定到DataGrid But also, Henk Holterman pointed out that I was overwriting my result set each iteration, which was a logic error. 而且, 亨克·霍尔特曼Henk Holterman)指出,我每次迭代都覆盖我的结果集,这是一个逻辑错误。

  • Henk's code seems to work the best so far, but I need to do more testing. 到目前为止,Henk的代码似乎效果最好,但是我需要做更多的测试。

  • Spinon's answer also helped bring clarity to my mind, but his code gave me an error. Spinon的回答也使我头脑清晰,但他的代码给了我一个错误。

  • I need to try to understand Timwi's code better, but in it's current form, it did not work for me. 我需要尝试更好地理解Timwi的代码,但是按照目前的形式,它对我不起作用。

NEW CODE 新密码

DataTable table = new DataTable();

foreach (Characteristic c in characteristics)
{
  EnumerableRowCollection<DataRow> rows = null;

  if (c.IsIncluded)
  {
    rows = (from r in MyTable.AsEnumerable()
             where r.Field<string>(c.Name) == c.Value
             select r);
  }
  else
  {
    rows = (from r in MyTable.AsEnumerable()
             where r.Field<string>(c.Name) != c.Value
            select r);
  }

  table.Merge(rows.CopyToDataTable());
}

dataGrid.DataContext = table;

The logic in your posting is wonky; 您发布的逻辑很奇怪; here is my attempt of what I think you are trying to achieve. 这是我对您认为要实现的目标的尝试。

DataTable table = MyTable.AsEnumerable()
     .Where(r => characteristics.All(c => !c.IsIncluded ||
                                          r.Field<string>(c.Name) == c.Value))
     .CopyToDataTable();

If you actually want to use the logic in your posting, change || 如果您实际上想在发布中使用逻辑,请更改|| to ^ , but that seems to make little sense. ^ ,但这似乎毫无意义。

You overwrite the table variable for each characteristic, so in the end it only holds the results from the last round, and that that apparently is empty. 您覆盖了每个特征的table变量,因此最后它仅保留上一轮的结果,而且显然是空的。

What you could do is something like: 您可以做的是:

// untested
var t = q.CopyToDataTable();
table.Merge(t);

And I suspect your query should use MyTable as the source: 而且我怀疑您的查询应使用MyTable作为来源:

var q = (from r in MyTable.AsEnumerable() ...

But that's not entirely clear. 但这还不是很清楚。

If you are trying to just insert the rows into your table then try calling the CopyToDataTable method this way: 如果您只是尝试将行插入表中,请尝试通过以下方式调用CopyToDataTable方法:

q.CopyToDataTable(table, LoadOption.PreserveChanges);

This way rather than reassigning the table variable you can just update it with the new rows that are to be inserted. 这样,您无需重新分配表变量,而只需使用要插入的新行对其进行更新即可。

EDIT: Here is an example of what I was talking about: 编辑:这是我正在谈论的一个示例:

DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Value", typeof(string));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM