2017-01-18 12 views
-1

私は2つのリストを持っており、以下のように購入しています。Linq Unionは動作しません

List<GroupDate> rca = (from sold in GetSoldOut 
            group sold by new { sold.CreatedDate, sold.SubCategoryID } 
             into g 
            select new GroupDate 
            { 
             Date = g.Key.CreatedDate, 
             SubCategoryID = g.Key.SubCategoryID, 
             Count = g.Count() 
            }).ToList(); 

List<GroupDate> purchase = (from sold in stock 
             group sold by new { sold.CreatedDate, sold.SubCategoryID } 
             into g 
             select new GroupDate 
             { 
              Date = g.Key.CreatedDate, 
              SubCategoryID = g.Key.SubCategoryID, 
              Count = g.Sum(a => a.Quantity) 
             }).ToList(); 

そして、次のように、この二つのリストに参加。

var leftOuterJoinRP = from first in replace 
           join last in prepaid 
           on new { first.Date, first.SubCategoryID } equals new { last.Date, last.SubCategoryID } 
           into temp 
           from last in temp.DefaultIfEmpty(new GroupDate { }) 
           select new CardBalance 
           { 
            Date = first.Date, 
            SubCategoryID = first.SubCategoryID, 
            ReDemage = first.Count, 
            Prepaid = last.Count 
           }; 
     var rightOuterJoinRP = from last in prepaid 
           join first in replace 
           on new { last.Date, last.SubCategoryID } equals new { first.Date, first.SubCategoryID } 
           into temp 
           from first in temp.DefaultIfEmpty(new GroupDate { }) 
           select new CardBalance 
           { 
            Date = last.Date, 
            SubCategoryID = last.SubCategoryID, 
            ReDemage = first.Count, 
            Prepaid = last.Count 
           }; 

leftOuterJoinRPは

Date---| Balance | OpeningStock | Prepaid | Purchase | RCA | Demage | SubCategoryId 
1/1/17 | 0-------| 0----------- | 1------ | 600 -----| 2-- | 0 ---- | 84 


が含まれていrightOuterJoinRPは、次のように

Date---| Balance | OpeningStock | Prepaid | Purchase | RCA | Demage | SubCategoryId 
1/1/17 | 0-------| 0----------- | 1------ | 600-----| 2-- | 0 ---- | 84 
1/2/17 | 0-------| 0----------- | 1------ | 110-----| 1-- | 0 ---- | 84 

連合leftOuterJoinRPとrightOuterJoinRPが含まれています。

var fullOuterJoinRP = leftOuterJoinRP.Union(rightOuterJoinRP); 

しかし、それは組合ではありません。 fullOuterJoinRPすべての行を取得します。

答えて

1

IEqualityComparer<T>パラメータを取るUnionメソッドを使用する必要があります。

のは、あなたがTestClass

public class TestClass 
{ 
    public int TestInteger { get; set; }  
    public string TestString { get; set; } 
} 

を持っていると言う、ここで二つのリスト

List<TestClass> list1 = new List<TestClass>(); 
list1.Add(new TestClass() { TestInteger = 1, TestString = "t1" }); 
list1.Add(new TestClass() { TestInteger = 2, TestString = "t2" }); 

List<TestClass> list2 = new List<TestClass>(); 
list2.Add(new TestClass() { TestInteger = 1, TestString = "t1" }); 
list2.Add(new TestClass() { TestInteger = 3, TestString = "t3" }); 

IEnumerable<TestClass> list3 = list1.Union(list2); 

を作成してみましょう、Union方法は、あなたの質問のように、4つのすべてのオブジェクトを返します。

Unionメソッドはオブジェクトを比較するためにIEqualityComparer<TestClass>パラメータが必要です。

public class TestClassComparer : IEqualityComparer<TestClass> 
{ 
    public bool Equals(TestClass x, TestClass y) 
    { 
     //Check whether the objects are the same object. 
     if (Object.ReferenceEquals(x, y)) return true; 

     //Check whether the class properties are equal. 
     return x != null && y != null && x.TestInteger.Equals(y.TestInteger) && x.TestString.Equals(y.TestString); 
    } 

    public int GetHashCode(TestClass obj) 
    { 
     //Get hash code for the TestString field if it is not null. 
     int hashTestString = obj.TestString == null ? 0 : obj.TestString.GetHashCode(); 

     //Get hash code for the TestInteger field. 
     int hashTestInteger = obj.TestInteger.GetHashCode(); 

     //Calculate the hash code for the TestClass object. 
     return hashTestString^hashTestInteger; 
    } 
} 

さて、あなたは

IEnumerable<TestClass> list3 = list1.Union(list2, new TestClassComparer()); 
を呼び出す場合は、結果 list3は、3つのユニークなオブジェクトを持つことになります。

+0

正常に動作します。ありがとう、HebeleHododo –

関連する問題