如何删除查询中的重复代码?

时间:2013-12-27 07:48:49

标签: c# linq linq-to-entities

此查询使用 distinct 但未删除重复的代码。 实际上,重复代码的主要原因是评论。 在 UserTimeLineComments 部分的查询中出现重复的代码。

    var UserTimeLineNews = (from l in _newsService.NewsQuery()
                            where l.UserId == UserId && l.IsActive == true
                            orderby l.CreateDate descending
                            select new UserTimeLine
                            {
                                EventDate = l.CreateDate,
                                CreateDate = l.CreateDate,
                                NewsId = l.NewsId,
                                TimeLineType = TimeLineType.CreateNews,
                                Title = l.Title,
                                Abstract = l.NewsAbstract,
                                CommentContent =null,
                                CommentCount = l.CommentCount,
                                LikeCount = l.LikeCount,
                                ViewsCount = l.ViewsCount,
                                Storyteller = l.Storyteller
                            }).AsQueryable();//Take(NumberOfNews).ToList();

    var UserTimeLineLikeNews = (from l in _likeNewsService.LikeNewsQueryable()
                                where l.UserId == UserId
                                orderby l.CreateDate descending
                                select new UserTimeLine
                                {
                                    EventDate = l.CreateDate,
                                    CreateDate = l.CreateDate,
                                    NewsId = l.NewsId,
                                    TimeLineType = TimeLineType.LikeNews,
                                    Title = l.News.Title,
                                    Abstract = l.News.NewsAbstract,
                                    CommentContent =null,
                                    CommentCount = l.News.CommentCount,
                                    LikeCount = l.News.LikeCount,
                                    ViewsCount = l.News.ViewsCount,
                                    Storyteller = l.News.Storyteller
                                }).AsQueryable();//Take(NumberOfNews).ToList();

    var UserTimeLineComments = (from l in _commentService.CommentQueryable()
                                where l.UserId == UserId && l.IsActive == true
                                orderby l.CreateDate descending
                                select new UserTimeLine
                                {
                                    EventDate = l.CreateDate,
                                    CreateDate = l.CreateDate,
                                    NewsId = l.NewsId,
                                    TimeLineType = TimeLineType.Comment,
                                    Title = l.News.Title,
                                    Abstract = l.News.NewsAbstract,
                                    CommentContent = l.Content,
                                    CommentCount = l.News.CommentCount,
                                    LikeCount = l.News.LikeCount,
                                    ViewsCount = l.News.ViewsCount,
                                    Storyteller = l.News.Storyteller
                                }).AsQueryable();//Take(NumberOfNews).ToList();

    var item = (UserTimeLineNews
        .Union(UserTimeLineLikeNews)
        .Union(UserTimeLineComments))
        .OrderByDescending(e => e.EventDate)
        .Distinct()
        .Take(NumberOfNews)
        .ToList();

我使用'使用UserTimeLine类实现IEquatable'但会出现错误消息。

LINQ to Entities无法识别方法'System.Linq.IQueryable1 [UserTimeLine] Distinct [UserTimeLine](System.Linq.IQueryable1 [UserTimeLine],System.Collections.Generic.IEqualityComparer`1 [UserTimeLine])'方法,并且此方法无法转换为商店表达式。

 public class DistinctNews : IEqualityComparer<UserTimeLine>
 {
    public bool Equals(UserTimeLine x, UserTimeLine y)
    {
        return x.NewsId==y.NewsId;
    }

    public int GetHashCode(UserTimeLine obj)
    {
        return obj.NewsId.GetHashCode();
    }
  }

 var item = (UserTimeLineNews
        .Union(UserTimeLineLikeNews)
        .Union(UserTimeLineComments))
        .OrderByDescending(e => e.EventDate)
        .Distinct(new DistinctNews())
        .Take(NumberOfNews)
        .ToList();

我使用'使用UserTimeLine类实现IEquatable'但重复代码未被删除

public class UserTimeLine: IEquatable<UserTimeLine>
{
    public long NewsId { set; get; }
    public string Title { set; get; }
    //and other property        

    public bool Equals(UserTimeLine other)
    {  
        if (Object.ReferenceEquals(other, null)) return false;

        if (Object.ReferenceEquals(this, other)) return true;

        return NewsId.Equals(other.NewsId) && Title.Equals(other.Title);
    }

    public override int GetHashCode()
    {  
        int hashNewsTitel = Title == null ? 0 : Title.GetHashCode();

        int hashNewsId = NewsId.GetHashCode();

        return hashNewsTitel ^ hashNewsId;
    }
}

1 个答案:

答案 0 :(得分:0)

LINQ的Distinct()方法使用默认的相等比较器,对于像UserTimeLine类这样的引用类型,它检查引用相等性。当您在查询中创建不同的对象时,这些对象都是不同的。

你需要

a)使用IEquatable<T>

实施UserTimeLine
class UserTimeLine: IEquatable<UserTimeLine>
{
    public int NewsId { get; set; }
    /* other properties omitted for brevity */

    public bool Equals(UserTimeLine other) {
        return this.NewsId == other.NewsId;
    }
}

b)或使用Distinct重载,它需要进行比较的IEqualityComparer<T>