Linq Union不起作用

时间:2017-01-18 05:14:03

标签: c# linq

我有两个rca列表并购买如下。

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   

Union leftOuterJoinRP和rightOuterJoinRP如下。

var fullOuterJoinRP = leftOuterJoinRP.Union(rightOuterJoinRP);  

但它没有结合。 fullOuterJoinRP获取所有行。

1 个答案:

答案 0 :(得分:1)

您需要使用带有Union参数的IEqualityComparer<T>方法。

我们假设你有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方法将返回所有四个对象,例如您的问题。

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将有三个唯一对象。