【问题标题】:EF GroupBy with concatenate string带有连接字符串的 EF GroupBy
【发布时间】:2015-06-04 11:38:49
【问题描述】:

我有一个类似下面的表格条目,

ColumnA       ColumnB      ColumnC      ColumnD

16             651           a          European Union 
16             651           x          Membership
17             651           a          Great Britain 
17             651           x          Economic integration
18             651           a          European Union countries 
18             651           x          Economic integration

结果列表

ColumnA                ColumnB

651                    |a European Union|x Membership
651                    |a European Union countries|x Economic integration
651                    |a Great Britain|x Economic integration 

想法是将3列分组,即ColumnA,ColumnB,ColumnC和Concatenate ColumnD。

到目前为止我已经做到了

var sub = (from x in db.Bs
                   where x.BId == BId
                   group x by new { x.BId, x.Sd, x.TNo, x.Tr } into g
                   select new Book
                   {
                       Tagno = g.Key.TNo,
                       value = " |" + g.Key.Sd + " " + g.Key.   // I am not able to get the Value column here.
                   });

【问题讨论】:

  • 因为它是分组的,我们可以使用下面的语法 g.Select(exp=>exp.ValueColumn) 获取该组下的值列表。
  • 抛出异常Values of type 'collection[Edm.String(Nullable=True,DefaultValue=,MaxLength=Max,Unicode=True,FixedLength=False)]' can not be converted to string."
  • 您需要使用 string.join("|",g.Select(exp=>exp.ValueColumn)) 因为分组结果将是一个列表,我们需要使用 string.join 进行连接列表中的值
  • 实体框架不支持String.Join
  • 有什么问题吗? String.Join 与实体框架无关,我们所做的与我们将如何使用加入字符加入集合的方式相同

标签: c# entity-framework


【解决方案1】:

这是为您准备的完整程序。 班级:

public class TestGroup
{
    public Int16 ColumnA { get; set; }
    public Int16 ColumnB { get; set; }
    public String ColumnC { get; set; }
    public String ColumnD { get; set; }
}

方法:

    public static void GroupTest()
    {
        var testGroup = new List<TestGroup>();
        testGroup.Add(new TestGroup { ColumnA = 16, ColumnB = 651, ColumnC = "a", ColumnD = "European Union" });
        testGroup.Add(new TestGroup { ColumnA = 16, ColumnB = 651, ColumnC = "x", ColumnD = "Membership" });
        testGroup.Add(new TestGroup { ColumnA = 17, ColumnB = 651, ColumnC = "a", ColumnD = "Great Britain" });
        testGroup.Add(new TestGroup { ColumnA = 17, ColumnB = 651, ColumnC = "z", ColumnD = "Economic integration" });
        testGroup.Add(new TestGroup { ColumnA = 18, ColumnB = 651, ColumnC = "a", ColumnD = "European Union countries" });
        testGroup.Add(new TestGroup { ColumnA = 18, ColumnB = 651, ColumnC = "x", ColumnD = "Economic integration" });

        var test = (from x in testGroup
            group x by new {x.ColumnA, x.ColumnB}
            into grp select grp).ToList().Select(grp=> new
            {
                TagNo = grp.Key.ColumnB,
                Text = String.Join(" | ", grp.Select(y => y.ColumnC + " " + y.ColumnD))
            });
        foreach (var x in test)
        {
            Console.WriteLine(String.Format("Tag No: {0}\t Text : {1}", x.TagNo, x.Text));
        }
        Console.Read();
    }

【讨论】:

  • 分隔符 | 未正确附加。
  • 该行应该是Text = String.Join("", grp.Select(y =&gt; "|" +y.ColumnC + " " + y.ColumnD))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 2022-04-06
  • 1970-01-01
相关资源
最近更新 更多