public class SpecialClass
{
public int property1;
public ListAttributes attributes = new ListAttributes();
}
public class ListAttributes
{
public List<a> aList = new List<a>();
public List<b> bList = new List<b>();
public List<c> cList = new List<c>();
public List<d> dList = new List<d>();
}
我有一个SpecialClass
个对象列表。我想对它们进行分组,以使具有相同SpecialClass
,aList
,bList
和cList
的每个dList
对象位于同一组中。
这些列表相等的条件可表示为:
var areEquivalent = !list1.Except(list2).Union(list2.Except(list1)).Any();
换句话说,这些不同的列表应该具有相同的成员,无论它们在列表中的位置如何。
如何使用LINQ完成此操作? (如果它可以表示为lambda表达式,但欢迎使用查询表达式,我将不胜感激)
答案 0 :(得分:2)
一种选择是编写自定义library(leaflet)
sp <- as(sf_combined, 'Spatial')
sf_combined %>%
leaflet() %>%
addTiles() %>%
addPolygons()
。这个示例可能会帮助您入门。
EqualityComparer
还可以考虑简化现有的比较,以使用原始问题评论中提到的using System;
using System.Collections.Generic;
using System.Linq;
public class ListAttributesEqualityComparer : IEqualityComparer<ListAttributes>
{
public bool Equals(ListAttributes x, ListAttributes y)
{
if (ReferenceEquals(x, y))
return true;
if (x == null || y == null)
return false;
// You should also do null checks on aList etc, but I have left those out for brevity
var result = x.aList.Count == y.aList.Count && x.bList.Count == y.bList.Count && x.cList.Count == y.cList.Count &&
x.dList.Count == y.dList.Count;
result = result && !x.aList.Except(y.aList).Union(y.aList.Except(x.aList)).Any();
result = result && !x.bList.Except(y.bList).Union(y.bList.Except(x.bList)).Any();
result = result && !x.cList.Except(y.cList).Union(y.cList.Except(x.cList)).Any();
result = result && !x.dList.Except(y.dList).Union(y.dList.Except(x.dList)).Any();
return result;
}
public int GetHashCode(ListAttributes obj)
{
return (obj?.aList?.Distinct()?.Count() ?? 0).GetHashCode();
}
}
class Program
{
static void Main(string[] args)
{
var list = new List<SpecialClass>();
// Populate list here
var results = list.GroupBy(g => g.attributes, new ListAttributesEqualityComparer());
Console.ReadLine();
}
}
public class SpecialClass
{
public int property1;
public ListAttributes attributes = new ListAttributes();
}
public class ListAttributes
{
public List<int> aList = new List<int>();
public List<string> bList = new List<string>();
public List<double> cList = new List<double>();
public List<DateTime> dList = new List<DateTime>();
}
。