LINQ ALL 方法仅在有一个条件要检查时才有效
LINQ ALL Method only works when there is one condition to check
我有一个 Dictionary<string, string>
,其中包含一些需要根据 DataTable
检查的条件。
例如如果 dic 包含一个条目,如 (key: "Email", value: "john.smith@gmail.com"),下面必须搜索 DataTable 中 Email 的值等于 "john.smith@gmail.com" 的所有行。
var foundRows = dtContacts.AsEnumerable()
.Where(c => dicConditions.All(kv => c.Field<string>(kv.Key) == kv.Value.ToString()));
当我们在字典中只有一个条件时,这是可行的。但我希望它也适用于更多条件。
例如,我现在想为 FirstName 的值为 "John" 且 [=25= 的值为 DataTable
的每个人签入 DataTable
]LastName 是 "Smith"(不区分大小写)。尽管我可以看到 DataTable
中有一行 FirstName = "John" 和 LastName = "Smith",上面的 LINQ 没有返回任何值。
我做错了什么?
这应该也适用于多种条件(字典中的条目)。
所以我唯一想到的是它目前区分大小写,但您明确要求不区分大小写的方法:
var foundRows = dtContacts.AsEnumerable()
.Where(row => dicConditions
.All(kv => String.Equals(row.Field<string>(kv.Key), kv.Value, StringComparison.InvariantCultureIgnoreCase)));
我有一个 Dictionary<string, string>
,其中包含一些需要根据 DataTable
检查的条件。
例如如果 dic 包含一个条目,如 (key: "Email", value: "john.smith@gmail.com"),下面必须搜索 DataTable 中 Email 的值等于 "john.smith@gmail.com" 的所有行。
var foundRows = dtContacts.AsEnumerable()
.Where(c => dicConditions.All(kv => c.Field<string>(kv.Key) == kv.Value.ToString()));
当我们在字典中只有一个条件时,这是可行的。但我希望它也适用于更多条件。
例如,我现在想为 FirstName 的值为 "John" 且 [=25= 的值为 DataTable
的每个人签入 DataTable
]LastName 是 "Smith"(不区分大小写)。尽管我可以看到 DataTable
中有一行 FirstName = "John" 和 LastName = "Smith",上面的 LINQ 没有返回任何值。
我做错了什么?
这应该也适用于多种条件(字典中的条目)。
所以我唯一想到的是它目前区分大小写,但您明确要求不区分大小写的方法:
var foundRows = dtContacts.AsEnumerable()
.Where(row => dicConditions
.All(kv => String.Equals(row.Field<string>(kv.Key), kv.Value, StringComparison.InvariantCultureIgnoreCase)));