2012-06-18 9 views
0

내가이 도메인 객체가 있다고 가정 :LINQ 그룹을 병합 하시겠습니까?

DataPoint 
    ========= 
    public int Id {get;set;} 
    public int TypeId {get;set;} 
    public string Name {get;set;} 

    Connector 
    ========= 
    public DataPoint DataPoint1 {get;set;} 
    public int DataPoint1Id {get;set;} 
    public DataPoint DataPoint2 {get;set;} 
    public int DataPoint2Id {get;set;} 
    public string Name {get;set;} 
    public int TypeId {get;set;} 

내가 가지고 List<Connector> :

var groups = (from C in _Connectors 
        group C by new { C.DataPoint1Id, C.DataPoint2Id } 
        into cGroup            
        select new {cGroup.Key.DataPoint1Id, cGroup.Key.DataPoint2Id,  
    cGroup.ToList(), cGroup.Count()}); 

VAR 그룹이 보유 :

를 나는 다음과 같은 LINQ로 그룹화하고있어

List<Connector> _Connectors = new List<Connector>() 
    { 
    new Connector(){Name = "a", DataPoint1Id = 1, DataPoint2Id = 2},... 
    new Connector(){Name = "b", DataPoint1Id = 1, DataPoint2Id = 2},... 
    new Connector(){Name = "c", DataPoint1Id = 1, DataPoint2Id = 2},... 
    new Connector(){Name = "d", DataPoint1Id = 2, DataPoint2Id = 1},... 
    new Connector(){Name = "e", DataPoint1Id = 2, DataPoint2Id = 1}... 
    } 

DataPoint1Id = 1, DataPoint2Id = 2, list of connectors -"a","b","c", count = 3 
    DataPoint1Id = 2, DataPoint2Id = 1, list of connectors - "d","e", count = 2 

나는 일에이 두 객체를 병합하고 싶습니다 :

DataPoint1Id = 1, DataPoint2Id = 2, list of connectors - "a","b","c","d","e", count = 5 

내가 어떻게 달성 할 수 있습니까?

답변

1

에 대한

var groups = 
    from C in _Connectors 
    group C by new 
     { 
      Min = (int)Math.Min(C.DataPoint1Id, C.DataPoint2Id), 
      Max = (int)Math.Max(C.DataPoint1Id, C.DataPoint2Id) 
     } 
    into cGroup 
    select new 
     { 
      DataPoint1Id = cGroup.Key.Min, 
      DataPoint2Id = cGroup.Key.Max, 
      Connectors = cGroup.ToList(), 
      Count = cGroup.Count() 
     }; 
+0

대단히 감사합니다 어떻게, 나는 그것을 시도 할 것이다. – Shpongle

관련 문제