我有一个Dictionary<string, string>
,其中包含一些需要针对DataTable
进行检查的条件。
E.g。如果dic包含一个条目,如(键:&#34;电子邮件&#34;,值:&#34; john.smith@gmail.com"),以下内容必须搜索DataTable中的所有行电子邮件的值等于&#34; john.smith@gmail.com"。
var foundRows = dtContacts.AsEnumerable()
.Where(c => dicConditions.All(kv => c.Field<string>(kv.Key) == kv.Value.ToString()));
当我们在字典中只有一个条件时,这是有效的。但我预计它也适用于更多条件。
例如,我现在想要在DataTable
中查看 FirstName 的值是&#34; John&#34;和 LastName 的值是&#34; Smith&#34; (不区分大小写)。即使我可以看到我的DataTable
中有一行 FirstName =&#34; John&#34;和 LastName =&#34; Smith&#34;,上面的LINQ没有返回任何值。
我做错了什么?
答案 0 :(得分:2)
这也适用于多种情况(字典中的条目)。
因此,我唯一想到的是它目前区分大小写,但您明确要求采用不区分大小写的方法:
var foundRows = dtContacts.AsEnumerable()
.Where(row => dicConditions
.All(kv => String.Equals(row.Field<string>(kv.Key), kv.Value, StringComparison.InvariantCultureIgnoreCase)));