问题描述
有如下三张表,ID_A,ID_B,ID_C分别是ABC三张表的主键表AID_A123表BID_BID_AID_Ca1Ab1Bc2Cd2De3E表CID_CtextAhelloBboyCgoodDjobEsee想要实现的效果如下ID_Atexts1hello,boy2good,job3see这个用lamda怎么写,求助,用groupjoin写了半天就是获取不到texts
解决方案
解决方案二:
text直接取你肯定是取不到的,因为groupby后,text是一个数组
解决方案三:
staticvoidMain(string[]args){List<int>lstA=newList<int>(){1,2,3,4};List<CB>lstB=newList<CB>(){newCB(){ID_B="a",ID_A="1",ID_C="A"},newCB(){ID_B="b",ID_A="1",ID_C="B"},newCB(){ID_B="c",ID_A="2",ID_C="C"},newCB(){ID_B="d",ID_A="2",ID_C="D"},newCB(){ID_B="e",ID_A="3",ID_C="E"}};List<CC>lstC=newList<CC>(){newCC(){ID_C="A",Text="hello"},newCC(){ID_C="B",Text="boy"},newCC(){ID_C="C",Text="good"},newCC(){ID_C="D",Text="job"},newCC(){ID_C="E",Text="see"}};lstB.Join(lstC,x=>x.ID_C,x=>x.ID_C,(x,y)=>new{ID_A=x.ID_A,Texts=y.Text}).GroupBy(x=>x.ID_A).Select(x=>new{ID_A=x.First().ID_A,texts=string.Join(",",x.Select(y=>y.Texts))}).ToList().ForEach(x=>Console.WriteLine(x));Console.ReadLine();}classCB{publicstringID_B;publicstringID_A;publicstringID_C;}classCC{publicstringID_C;publicstringText;}
解决方案四:
staticvoidMain(string[]args){List<int>lstA=newList<int>(){1,2,3,4};List<CB>lstB=newList<CB>(){newCB(){ID_B="a",ID_A="1",ID_C="A"},newCB(){ID_B="b",ID_A="1",ID_C="B"},newCB(){ID_B="c",ID_A="2",ID_C="C"},newCB(){ID_B="d",ID_A="2",ID_C="D"},newCB(){ID_B="e",ID_A="3",ID_C="E"}};List<CC>lstC=newList<CC>(){newCC(){ID_C="A",Text="hello"},newCC(){ID_C="B",Text="boy"},newCC(){ID_C="C",Text="good"},newCC(){ID_C="D",Text="job"},newCC(){ID_C="E",Text="see"}};//ID_Atexts//1hello,boy//2good,job//3seevarquery=lstA.Where(obj=>{returnlstB.Where(obj2=>Convert.ToInt32(obj2.ID_A)==obj).Count()>0;}).Select(obj=>{varquery3=frombinlstBjoincinlstConb.ID_Cequalsc.ID_CwhereConvert.ToInt32(b.ID_A)==objselectc.Text;stringstr=string.Join(",",query3);returnnew{ID_A=obj,texts=str};});foreach(varoneinquery){}}classCB{publicstringID_B;publicstringID_A;publicstringID_C;}classCC{publicstringID_C;publicstringText;}
解决方案五:
classProgram{staticvoidMain(string[]args){List<ModelA>tableA=newList<ModelA>{newModelA{ID_A=1},newModelA{ID_A=2},newModelA{ID_A=3},};List<ModelB>tableB=newList<ModelB>{newModelB{ID_B="a",ID_A=1,ID_C="A"},newModelB{ID_B="b",ID_A=1,ID_C="B"},newModelB{ID_B="c",ID_A=2,ID_C="C"},newModelB{ID_B="d",ID_A=2,ID_C="D"},newModelB{ID_B="e",ID_A=3,ID_C="E"},};List<ModelC>tableC=newList<ModelC>{newModelC{ID_C="A",text="hello"},newModelC{ID_C="B",text="boy"},newModelC{ID_C="C",text="good"},newModelC{ID_C="D",text="job"},newModelC{ID_C="E",text="see"},};varresult=tableC.Join(tableB,c=>c.ID_C,b=>b.ID_C,(tA,tB)=>new{ID_C=tA.ID_C,text=tA.text,ID_A=tB.ID_A,}).GroupBy(g=>g.ID_A).Select(s=>new{ID_A=s.Key,texts=string.Join(",",s.Select(se=>se.text)),});//最后与表一jion。事实。我感觉有点多余}classModelA{publicintID_A{get;set;}}classModelB{publicstringID_B{get;set;}publicintID_A{get;set;}publicstringID_C{get;set;}}classModelC{publicstringID_C{get;set;}publicstringtext{get;set;}}}