Linq:不同的值我从XML中设置了以下项:id category5 15 35 45 35 3我需要这些物品的单独清单:5 15 35 4在LINQ中,我如何区分类别和ID?
3 回答
叮当猫咪
TA贡献1776条经验 获得超12个赞
var query = doc.Elements("whatever") .Select(element => new { id = (int) element.Attribute("id"), category = (int) element.Attribute("cat") }) .Distinct();
DistinctBy
DistinctBy.cs
:
public static IEnumerable<TSource> DistinctBy<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer) { HashSet<TKey> knownKeys = new HashSet<TKey>(comparer); foreach (TSource element in source) { if (knownKeys.Add(keySelector(element))) { yield return element; } } }
null
- 3 回答
- 0 关注
- 701 浏览
添加回答
举报
0/150
提交
取消