我正试图找到与LINQ的交叉点.
样品:
List<int> int1 = new List<int>() { 1,2 };
List<int> int2 = new List<int>();
List<int> int3 = new List<int>() { 1 };
List<int> int4 = new List<int>() { 1, 2 };
List<int> int5 = new List<int>() { 1 };
Run Code Online (Sandbox Code Playgroud)
想要返回:1,因为它存在于所有列表中..如果我运行:
var intResult= int1
.Intersect(int2)
.Intersect(int3)
.Intersect(int4)
.Intersect(int5).ToList();
Run Code Online (Sandbox Code Playgroud)
它返回什么,因为1显然不在int2列表中.无论一个列表是否为空,如何使其工作?
使用上面的例子或:
List<int> int1 = new List<int>() { 1,2 };
List<int> int2 = new List<int>();
List<int> int3 = new List<int>();
List<int> int4 = new List<int>();
List<int> int5 = new List<int>();
Run Code Online (Sandbox Code Playgroud)
在这种情况下如何返回1和2 ..如果列表已填充,我不知道提前...
Kob*_*obi 15
如果您只需一步即可完成,最简单的解决方案是过滤掉空列表:
public static IEnumerable<T> IntersectNonEmpty<T>(this IEnumerable<IEnumerable<T>> lists)
{
var nonEmptyLists = lists.Where(l => l.Any());
return nonEmptyLists.Aggregate((l1, l2) => l1.Intersect(l2));
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在列表或其他IEnumerables 的集合上使用它:
IEnumerable<int>[] lists = new[] { l1, l2, l3, l4, l5 };
var intersect = lists.IntersectNonEmpty();
Run Code Online (Sandbox Code Playgroud)
您可能更喜欢常规的静态方法:
public static IEnumerable<T> IntersectNonEmpty<T>(params IEnumerable<T>[] lists)
{
return lists.IntersectNonEmpty();
}
var intersect = ListsExtensionMethods.IntersectNonEmpty(l1, l2, l3, l4, l5);
Run Code Online (Sandbox Code Playgroud)
您可以编写一个扩展方法来定义该行为。就像是
static class MyExtensions
{
public static IEnumerable<T> IntersectAllIfEmpty<T>(this IEnumerable<T> list, IEnumerable<T> other)
{
if (other.Any())
return list.Intersect(other);
else
return list;
}
}
Run Code Online (Sandbox Code Playgroud)
所以下面的代码会打印 1。
List<int> list1 = new List<int>() { 1, 2 };
List<int> list2 = new List<int>();
List<int> list3 = new List<int>() { 1 };
foreach (int i in list1.IntersectAllIfEmpty(list2).IntersectAllIfEmpty(list3))
Console.WriteLine(i);
Run Code Online (Sandbox Code Playgroud)
更新:
匿名在对该问题的评论中提出了一个很好的观点。如果上面的函数本身是空的,那么它会产生一个空集list,这应该是可取的。这意味着如果方法链中的第一个列表或任何交集的结果集为空,则最终结果将为空。
要允许第一个列表为空,但不允许结果集为空,您可以采取不同的方法。该方法不是扩展方法,而是采用 IEnumerables 的 params 数组,并首先过滤掉空集,然后尝试与其余集相交。
public static IEnumerable<T> IntersectAllIfEmpty<T>(params IEnumerable<T>[] lists)
{
IEnumerable<T> results = null;
lists = lists.Where(l => l.Any()).ToArray();
if (lists.Length > 0)
{
results = lists[0];
for (int i = 1; i < lists.Length; i++)
results = results.Intersect(lists[i]);
}
else
{
results = new T[0];
}
return results;
}
Run Code Online (Sandbox Code Playgroud)
你会像这样使用它
List<int> list0 = new List<int>();
List<int> list1 = new List<int>() { 1, 2 };
List<int> list2 = new List<int>() { 1 };
List<int> list3 = new List<int>() { 1,2,3 };
foreach (int i in IntersectAllIfEmpty(list0, list1, list2, list3))
{
Console.WriteLine(i);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10645 次 |
| 最近记录: |