c# - EF GroupBy with concatenate string -
i have table entry below,
columna columnb columnc columnd 16 651 european union 16 651 x membership 17 651 great britain 17 651 x economic integration 18 651 european union countries 18 651 x economic integration
result list
columna columnb 651 |a european union|x membership 651 |a european union countries|x economic integration 651 |a great britain|x economic integration
the idea group 3 columns, ie, columna, columnb,columnc , concatenate columnd.
so far have done this
var sub = (from x in db.bs x.bid == bid group x new { x.bid, x.sd, x.tno, x.tr } g select new book { tagno = g.key.tno, value = " |" + g.key.sd + " " + g.key. // not able value column here. });
here complete program you. class:
public class testgroup { public int16 columna { get; set; } public int16 columnb { get; set; } public string columnc { get; set; } public string columnd { get; set; } }
the method:
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 new {x.columna, x.columnb} 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(); }
Comments
Post a Comment