LINQ GroupBy для нескольких полей типа ref; Custom EqualityComparer
Итак, я просмотрел около 20 примеров по этому поводу в SO и других местах, но не нашел ни одного, который охватывал бы то, что я пытаюсь сделать. Это -Могу ли я указать свой явный компаратор типа inline? - выглядит так, как мне нужно, но недостаточно далеко (или я не понимаю, как это сделать).
I have a List of LoadData, the LoadData object has fields of both reference and value types Need to group on a mixture of ref and value fields, project the output to an anonymous typeNeed (I think) to provide a custom IEqualityComparer to specify how to compare the GroupBy fields, but they are an anonymous type
private class LoadData
{
public PeriodEndDto PeriodEnd { get; set; }
public ComponentDto Component { get; set; }
public string GroupCode { get; set; }
public string PortfolioCode { get; set; }
}
Лучший запрос GroupBy, который я получил до сих пор:
var distinctLoads = list.GroupBy(
dl => new { PeriodEnd = dl.PeriodEnd,
Component = dl.Component,
GroupCode = dl.GroupCode },
(key, data) => new {PeriodEnd = key.PeriodEnd,
Component = key.Component,
GroupCode = key.GroupCode,
PortfolioList = data.Select(d=>d.PortfolioCode)
.Aggregate((g1, g2) => g1 + "," + g2)},
null);
Это группы, но есть еще дубликаты.
How can I specify custom code to compare the GroupBy fields? For example, the Components could be compared by Component.Code.