LINQ GROUPBY多参考类型字段;自EqualityComparer [英] LINQ GroupBy on multiple ref-type fields; Custom EqualityComparer

查看:159
本文介绍了LINQ GROUPBY多参考类型字段;自EqualityComparer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我已经通过大约20例看着这对SO和其他地方,但还没有找到一个涵盖我想要做的。这 - 我可以指定我明确的类型比较直列? - 看起来我需要什么,但还远远不够(或我不知道如何采取进一步)。




  • 我有LoadData的列表中,LoadData对象有两个参考和值类型的字段

  • 需要组上ref和值字段的混合物,项目输出到一个匿名类型

  • 需要(我认为)提供一个自定义的IEqualityComparer指定如何比较GROUPBY领域,但他们是一个匿名类型

     私有类LoadData 
    {
    公共PeriodEndDto PeriodEnd {搞定;组; }
    公共ComponentDto组件{搞定;组; }
    公共字符串GroupCode {搞定;组; }
    公共字符串PortfolioCode {搞定;组; }
    }




最好的GROUPBY查询我已经打算迄今:

  VAR distinctLoads = list.GroupBy(
DL = gt;新建{PeriodEnd = dl.PeriodEnd,
分量= dl.Component,
GroupCode = dl.GroupCode}
(键,数据)=>新建{PeriodEnd = key.PeriodEnd,
组件= key.Component,
GroupCode = key.GroupCode,
PortfolioList = data.Select(D => d.PortfolioCode)
.Aggregate((G1,G2)= GT; G1 + ,+ G2)},
零);

这组,但仍存在重复。




  1. 如何指定自定义代码比较GROUPBY领域?例如,组件可以通过Component.Code比拟的。


解决方案

这里的问题是你的密钥类型是匿名的,这意味着你不能声明一个类实现的IEqualityComparer< T> 该键的类型。虽然这将是的可能的写一个比较器,相比匿名类型在自定义的方式平等(通过一个通用的方法,代表和类型推断),它不会是非常愉快的。



这两个简单的选择可能是:




  • 请匿名类型只是工作通过覆盖等于 PeriodEndDto /的GetHashCode和 ComponentDto 。如果有你想要到处使用天然的平等,这可能是sanest选项。我建议实施 IEquatable< T> 以及

  • 请不要使用匿名类型分组 - 使用命名类型,然后你可以覆盖的GetHashCode 等于上,或者你可以写在正常的方式自定义相等比较



编辑: ProjectionEqualityComparer 将没有真正的工作。这将是虽然写类似的东西可行的 - 一种 CompositeEqualityComparer 这使您从几个投影+比较器对的相等比较的。与匿名类型相比,虽然这将是很丑陋。


So I've looked through about 20 examples on this on SO and elsewhere, but haven't found one which covers what I'm trying to do. This - Can I specify my explicit type comparator inline? - looks like what I need, but doesn't go far enough (or I don't understand how to take it further).

  • 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 type
  • Need (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; }
    }
    

The best GroupBy query I have going so far:

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);

This groups, but there are still duplicates.

  1. How can I specify custom code to compare the GroupBy fields? For example, the Components could be compared by Component.Code.

解决方案

The problem here is that your key type is anonymous, which means you can't declare a class that implements IEqualityComparer<T> for that key type. While it would be possible to write a comparator which compared anonymous types for equality in a custom manner (via a generic method, delegates and type inference), it wouldn't be terribly pleasant.

The two simplest options are probably:

  • Make the anonymous type "just work" by overriding Equals/GetHashCode in PeriodEndDto and ComponentDto. If there's a natural equality you'd want to use everywhere, this is probably the sanest option. I'd recommend implementing IEquatable<T> as well
  • Don't use an anonymous type for grouping - use a named type, and then you can either override GetHashCode and Equals on that, or you could write a custom equality comparer in the normal way.

EDIT: ProjectionEqualityComparer wouldn't really work. It would be feasible to write something similar though - a sort of CompositeEqualityComparer which allowed you create an equality comparer from several "projection + comparer" pairs. It would be pretty ugly compared with the anonymous type though.

这篇关于LINQ GROUPBY多参考类型字段;自EqualityComparer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆