【问题标题】:Make a specific grouping method in class在课堂上制定特定的分组方法
【发布时间】:2019-01-31 08:18:32
【问题描述】:

我有一个对象<Repere>,其结构如下:

class Repere
{
    public string Name { get; set; }
    public List<Operation> Operations { get; set; }
    public int Quantite {get;set;}
    ...
}

当我想按名称对List&lt;Repere&gt; 进行分组时,我总是使用以下分组方法:

List<Repere> liste_rep_group = liste_rep.GroupBy(l => l.Name)
    .Select(cl => new Repere
    {
        Quantite = cl.Sum(c => c.TotalQuantity),
        TotalQuantity = cl.Sum(c => c.TotalQuantity),
        ID = -1,
        IdAff = cl.First().IdAff,
        Name = cl.First().Name,
        NameOri = cl.First().Name,
        Nom_aff = cl.First().Nom_aff,
        Profil = cl.First().Profil,
        Longueur = cl.First().Longueur,
        Hauteur = cl.First().Hauteur,
        Largeur = cl.First().Largeur,
        Poids = cl.First().Poids,
        Priorite = cl.Min(c => c.Priorite),
        Matiere = cl.First().Matiere,
        Angle1 = cl.First().Angle1,
        Angle2 = cl.First().Angle2,
        AngleAile1 = cl.First().AngleAile1,
        AngleAile2 = cl.First().AngleAile2,
        GroupeProfil = cl.First().GroupeProfil,
        ListOperations = (cl.SelectMany(g=>g.ListOperations).GroupBy(o=>o.ID)
            .Select(go => new Operation
                {
                    ID = go.First().ID,
                    QtyFinished = go.Sum(o => o.QtyFinished),
                    Color=go.First().Color,
                })).ToList()
        ...
    }).ToList();

我想做的是在我的类 Repere 中添加类似的方法

public List<Repere> GroupByName(List<Repere>)
{
   //My grouping method
}

所以每次我需要对我的List&lt;Repere&gt;进行分组时,我不需要到处复制所有的功能。主要原因是当我修改Repere结构时,我需要编辑功能,并且有忘记的风险在某处更新它。

【问题讨论】:

  • 好吧,那你为什么不呢?你的问题是什么?您可以将IQueryable&lt;Repere&gt; 作为参数传递给您的函数。
  • 您可以创建静态辅助方法或扩展方法。 @nbokmans 我认为没有必要
  • @nbokmans 因为我不知道该怎么做,如果您至少可以给我一些“关键词”,那么我会知道要搜索什么?已经开始寻找什么是 IQueryable,谢谢
  • @Jota.Toledo 然而这是最动态的解决方案。 IQueryable 是尚未解析为结果的数据库查询(通常由ToList()ToArray() 调用完成)。因此,在某些情况下,您希望对要返回的 Repere 集应用过滤(Where 子句),通常在数据库级别进行过滤比在内存中进行过滤更好。
  • @Siegfried.V 我相信,由于这个函数涉及我的类 Repere,您目前正在做的与类实例直接相关的一件事是 克隆 操作。您可以将其封装到 static Repere FromInstance(Repere source) 类方法中。分组操作本身与Repere 实例的集合有关,因此扩展方法会很方便,但不是强制性的。

标签: c# linq methods


【解决方案1】:

您可以在List&lt;Repere&gt; 上编写扩展方法来实现预期的行为。

public static class ListRepereExtensionMethods
{
    public static List<Repere> GroupByName(this List<Repere> liste_rep)
    {
        List<Repere> liste_rep_group = liste_rep.GroupBy(l => l.Name)
            .Select(cl => new Repere
            {
                Quantite = cl.Sum(c => c.TotalQuantity),
                TotalQuantity = cl.Sum(c => c.TotalQuantity),
                ID = -1,
                IdAff = cl.First().IdAff,
                Name = cl.First().Name,
                NameOri = cl.First().Name,
                Nom_aff = cl.First().Nom_aff,
                Profil = cl.First().Profil,
                Longueur = cl.First().Longueur,
                Hauteur = cl.First().Hauteur,
                Largeur = cl.First().Largeur,
                Poids = cl.First().Poids,
                Priorite = cl.Min(c => c.Priorite),
                Matiere = cl.First().Matiere,
                Angle1 = cl.First().Angle1,
                Angle2 = cl.First().Angle2,
                AngleAile1 = cl.First().AngleAile1,
                AngleAile2 = cl.First().AngleAile2,
                GroupeProfil = cl.First().GroupeProfil,
                ListOperations = (cl.SelectMany(g=>g.ListOperations).GroupBy(o=>o.ID)
                    .Select(go => new Operation
                        {
                            ID = go.First().ID,
                            QtyFinished = go.Sum(o => o.QtyFinished),
                            Color=go.First().Color,
                        })).ToList()
                ...
            }).ToList();
    }
}

使用上述扩展方法时,可以像这样调用方法

List&lt;Repere&gt; liste_rep_group = liste_rep.GroupByName();

您也可以选择将其实现为静态实用程序类。在这种情况下,您将从方法的参数部分中删除 this 并调用该方法,如

List&lt;Repere&gt; liste_rep_group = ListRepereExtensionMethods.GroupByName(liste_rep);

有关该方法的优缺点的一些讨论可在 Extension Methods vs Static Utility Class evaluating cost/benefits of using extension methods in C# => 3.0

【讨论】:

  • 谢谢,完美运行,不知道这些扩展方法:)
猜你喜欢
  • 2019-01-03
  • 1970-01-01
  • 2021-02-05
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多