【问题标题】:Group Linq Query Results in a String将 Linq 查询结果分组到字符串中
【发布时间】:2017-08-31 01:09:02
【问题描述】:

这是我想要完成的:

我有一份提供不同服务的公司列表。我正在尝试以字符串格式将一家公司的服务组合在一起,这样当我可以导出到 Excel 时,它会显示在一个列中。

现在,如果一家公司有 4 项服务,它们将在查询中显示 4 次不同的时间。这是合乎逻辑的,只是想将它们组合在一起。

这是我尝试得到的“无法将带有语句体的 lambda 表达式转换为表达式树”

Services = (from cc in CharityCategories join c in Cao_Categories on cc.CategoryID equals c.CategoryID
                        join chy in CharityYears on cc.CharityYearID equals chy.CharityYearID
                        where chy.CampYearID == 5 && chy.StatusID == 1
                        group c by c.Category into cg
                        select new { Categories = cg.Key.Trim()}).Aggregate(new StringBuilder(), (a, b) =>
                       {
                               if (a.Length > 0)
                                      a.Append(",");
                               a.Append(b.ToString().Split('=')[1].Replace(" }", ""));
                               return a;
                        }).ToString() ,

LinqPad 在StringBuilder()、“Aggregate(new StringBuilder(), (a, b)”之后显示错误信息

我可以让他们在一个链接中分组,当点击该链接时,该链接会以 { myservice = 1} 之类的格式列出他们 - 这就是我使用 .Append

的原因

【问题讨论】:

  • 您能否提供一些原始数据和预期输出。您代码中的逻辑确实无助于理解。
  • 这是我想要查询的内容:A 公司 - 电力、管道、甲板

标签: c# linq


【解决方案1】:

使用下面的代码,然后相应地改变你的逻辑,希望这会起作用

cg.Key.ToString().Trim() 中的修正

Services = (from cc in CharityCategories
                            join c in Cao_Categories on cc.CategoryID equals c.CategoryID
                            join chy in CharityYears on cc.CharityYearID equals chy.CharityYearID
                            where chy.CampYearID == 5 && chy.StatusID == 1
                            group c by c.Category into cg
                            select new { Categories = cg.Key.ToString().Trim() }).Aggregate(new StringBuilder(), (a, b) =>
                            {
                                if (a.Length > 0)
                                    a.Append(",");
                                a.Append(b.ToString().Split('=')[1].Replace(" }", ""));
                                return a;
                            }).ToString();

我不知道你的要求,如果你能提供准确的预期结果,我们可以纠正这个问题

【讨论】:

    【解决方案2】:

    正如错误所说,您不能将带有语句体的 lambda 表达式转换为表达式树。实体框架使用表达式树转换为 sql 语句。但是,您可以先使用 (AsEnumerable) 将结果具体化,然后将您的代码与 linq to objects 一起使用。

    Services = (from cc in CharityCategories
                            join c in Cao_Categories on cc.CategoryID equals c.CategoryID
                            join chy in CharityYears on cc.CharityYearID equals chy.CharityYearID
                            where chy.CampYearID == 5 && chy.StatusID == 1
                            group c by c.Category into cg
                            select new { Categories = cg.Key.ToString().Trim() })
                            .AsEnumerable()
                            .Aggregate(new StringBuilder(), (a, b) =>
                            {
                                if (a.Length > 0)
                                    a.Append(",");
                                a.Append(b.ToString().Split('=')[1].Replace(" }", ""));
                                return a;
                            }).ToString();
    

    重要提示:聚合将在从数据库中检索到所有行之后进行。

    【讨论】:

      【解决方案3】:

      假设你的结果集可以用如下类的形式表示

      public class Category
      {
           public string Name{get;set;}
           public string Product{get;set;}
      }
      

      您可以编写以下 LINQ 查询以按照您希望的方式获得结果:

      var testList = new List <Category> {
      new Category {Name = "A",Product = "decks"},
      new Category {Name = "B",Product = "cards"},
      new Category {Name = "C",Product = "paper"},
      new Category {Name = "A",Product = "scissor"},
      new Category {Name = "B",Product = "crates"},
      new Category {Name = "C",Product = "rocks"}
      };
      
      var finalList = testList
          .GroupBy(x => x.Name)
          .Select(x => new {
              Category =x.Key,
              Items = String.Join(",", x.Select(y => y.Product))
           });
      

      因此结果集的形式为:

      1. A 类 -> 甲板,剪刀
      2. B 类-> 卡片、板条箱
      3. C 类-> 纸、岩石

      如果有帮助,请标记为答案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-06
        • 1970-01-01
        • 1970-01-01
        • 2018-11-29
        相关资源
        最近更新 更多