在 T-SQL 中,您可以使用CROSS APPLY
从语句中获取表左右之间所有可能的变化。现在我遇到了以下情况C#
,我希望有一种方法可以使用 LINQ-to-Objects 来解决我的问题。
我有一个包含TestData
对象的列表(如下所示),它类似于KeyValuePair<string, object>
对象(只是一个Key
和一个Value
属性):键可以是所有内容,并且可以有多个具有相同键的对象。
IList<KeyValuePair<String, Object>> objects;
// Content of list
// # | Key | Value
// 1 | "A" | 1
// 2 | "A" | 2
// 3 | "A" | 3
// 4 | "B" | 4
// 5 | "B" | 5
// 6 | "C" | 6
// 7 | "D" | 7
// 8 | "D" | 8
我还有一个请求的密钥列表:
IList<String> requestedKeys = new List<string>() { "A", "D" };
现在我想在requestedKeys
列表中的键之间拥有 KeyValuePair 对象的所有可能组合。
IList<IList<KeyValuePair<String, Object>>> result = ...
// Content of 'result' will be in this example 6 lists with each 2 KeyValuePair objects
// # | "A" | "D" | (If there are more in the requestedKey list then there are more KeyValuePair items in the innerlist.)
// 1 | 1 | 7 |
// 2 | 2 | 7 |
// 3 | 3 | 7 |
// 4 | 1 | 8 |
// 5 | 2 | 8 |
// 6 | 3 | 8 |
是否可以使用 LINQ-to-Objects 解决我的问题。如果不是,你能告诉我最有效的构建方法吗?
编辑 1:
为了更清楚结果应该是什么:
我想要一个类似这样的 LINQ-to-Objects 查询:
@Joanna 感谢关于多个from
s 的提示,但问题是:使用这种语法,你不能有一个动态的from
s 的数量 就我而言,我需要与列表from
中的项目一样多的 srequestedKeys
var result =
from listA in objects.Where(m => m.Key == "A")
from listD in objects.Where(m => m.Key == "D")
// from .....
// from .....
// overhere as many froms as items in 'requestedKeys' list
select new [] { listA, listD /*, All other lists */ }