日期:2014-05-17 浏览次数:20749 次
只写了一个,, List<int> age = new List<int>(); for (int i = 0; i < AllNames.Count; i++) { if (Oname.Contains(AllNames[i])) { for (int j = 0; j < Oname.Count(); j++) { if (AllNames[i] == Oname[j]) { age.Add(Onum[j]); break; } } } else { age.Add(0); } }
------解决方案--------------------
原理其实是一样的
------解决方案--------------------
void Main() { string[] name={"张三","李四"}; int[] num={10,20}; string[] Oname={"张三","王五"}; int[] Onum={20,20}; string[] AllNames={"张三","李四","王五"} ; var a1=from n in name.Select((x,i)=>new {x,i}) join u in num.Select((y,i)=>new{y,i}) on n.i equals u.i select new {n.x,u.y}; var a2=from n in Oname.Select((x,i)=>new {x,i}) join u in Onum.Select((y,i)=>new{y,i}) on n.i equals u.i select new {n.x,u.y}; var query= a1.Concat(a2).GroupBy(s=>s.x).Select(s=>new {s.Key,i=s.Sum(z=>z.y)}); num=(from q in query join n in name on q.Key equals n into t from n in t.DefaultIfEmpty() select n==null?0:q.i).ToArray(); Onum=(from q in query join n in Oname on q.Key equals n into t from n in t.DefaultIfEmpty() select n==null?0:q.i).ToArray(); }
------解决方案--------------------
//更正一下: num=(from q in query join n in a1 on q.Key equals n.x into t from n in t.DefaultIfEmpty() select n==null?0:n.y).ToArray(); Onum=(from q in query join n in a2 on q.Key equals n.x into t from n in t.DefaultIfEmpty() select n==null?0:n.y).ToArray();