0

I want to write query more efficient.

I do not want before the end of the query, the list of data to extract.

    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,
                                    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,
                                        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();

After running the following error appears

Error: The type 'UserTimeLine' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.

3
  • 1
    Could you post your actual code, not just pseudo code? Commented Dec 26, 2013 at 12:23
  • The original code is written exactly the same way. Just y and z by x are related Commented Dec 26, 2013 at 12:30
  • Classes in C# are certainly not declared like you have it. Commented Dec 26, 2013 at 12:52

1 Answer 1

1

The first two queries don't initialize the CommentContent property. Add that to the initializer in the first two queries (or remove it in the last query) and the final query should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.