LINQ中两个列表的联合不起作用

时间:2016-02-22 07:30:59

标签: c# asp.net-mvc entity-framework linq union

我是一种方法,我会显示数据库中存在的书籍以及为借出的书籍。

当用户没有退回书籍时,书籍就会变为非活动状态。所以我写了一个LINQ查询,新用户可以选择一本书。然后,如果用户选择了丢失的书。他将会发出一条消息,说明该书不可用。

我在这里使用联盟找到活跃的书籍和借出但不归还的书籍。

但是重复的值在方法中返回。

public IEnumerable<UsageType> GetUsageType(int BookID = 0)
        {
            _db.Configuration.ProxyCreationEnabled = false;
            List<UsageType> Uresult = new List<UsageType>();
            List<UsageType> result = new List<UsageType>();
            try
            {
                if (BookID == 0)
                {
                    result = (from Usage in _db.USAGE_TYPE
                              where Usage.IS_ACTIVE == true
                              select new
                              {
                                  UsageTypeId = Usage.USAGE_TYPE_ID,
                                  UsageTypeName = Usage.USAGE_TYPE_NAME,
                                  IsActive = Usage.IS_ACTIVE
                              }).AsEnumerable()
                                          .Select(x => new UsageType
                                          {
                                              UsageTypeId = x.UsageTypeId,
                                              UsageTypeName = x.UsageTypeName,
                                              IsActive = x.IsActive.HasValue ? x.IsActive.Value : false
                                          }).Distinct().ToList();
                }
                else
                {
                    result = (from Usage in _db.USAGE_TYPE
                              where Usage.IS_ACTIVE == true
                              select new
                              {
                                  UsageTypeId = Usage.USAGE_TYPE_ID,
                                  UsageTypeName = Usage.USAGE_TYPE_NAME,
                                  IsActive = Usage.IS_ACTIVE
                              }).AsEnumerable()
                                     .Select(x => new UsageType
                                     {
                                         UsageTypeId = x.UsageTypeId,
                                         UsageTypeName = x.UsageTypeName,
                                         IsActive = x.IsActive.HasValue ? x.IsActive.Value : false
                                     }).ToList();

                    var savedUsagetype = (from Usage in _db.USAGE_TYPE
                                          join usageCategory in _db.USAGE_CATEGORY on Usage.USAGE_TYPE_ID equals usageCategory.USAGE_TYPE_ID
                                          join book_usage in _db.BOOK_USAGE
                                          on usageCategory.USAGE_CATEGORY_ID equals book_usage.USAGE_CATEGORY_ID
                                          where book_usage.Book_ID == BookID 
                                          select new
                                          {
                                              UsageTypeId = Usage.USAGE_TYPE_ID,
                                              UsageTypeName = Usage.USAGE_TYPE_NAME,
                                              IsActive = Usage.IS_ACTIVE
                                          }).AsEnumerable()
                                       .Select(x => new UsageType
                                       {
                                           UsageTypeId = x.UsageTypeId,
                                           UsageTypeName = x.UsageTypeName,
                                           IsActive = x.IsActive.HasValue ? x.IsActive.Value : false
                                       }).ToList();



                Uresult= result.Union(savedUsagetype).Distinct().ToList();
                }

                return Uresult;
            }
            catch (Exception ex)
            {                
                return null;
            }
        }

1 个答案:

答案 0 :(得分:2)

您正在比较自己类的对象,因此您应该在IEquatable类中实现UsageType,以告知linq 如何比较您的对象。例如:

public class UsageType : IEquatable<UsageType>
{
    public int UsageTypeId {get; set;}
    ...

    public bool Equals(UsageType other)
    {
       return this.UsageTypeId == other.UsageTypeId;
    }
    public override bool Equals(object other)
    {
       return this.UsageTypeId == ((UsageType)other).UsageTypeId;
    }
    public override int GetHashCode()
    { 
       return this.UsageTypeId.GetHashCode();
    }
}

现在,您可以使用UsageType获取IEqualityComparer<UsageType>的实例:

Uresult = result.Union(savedUsageType,IEqualityComparer<UsageType>.Default).ToList();

注意:也始终覆盖object.Equalsobject.GetHashcode()。如果不这样做,仍旧使用非通用Enumerable的旧代码会产生意外结果,因为它们使用其他逻辑进行比较而不是您期望的结果。