EntityFramework核心联盟选择 - 空相关数据

时间:2017-04-14 12:46:45

标签: linq asp.net-core entity-framework-core

为了获得更好的图片,我正在尝试使用不同元素组成的Feed(我会尝试尽可能缩短示例):

public class FeedDTO
    {
        public int Type { get; set; }
        public DateTime Date { get; set; }

        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public CommentDTO Comment { get; set; }

        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public LikeDTO Like { get; set; }

        public static FeedDTO SetComment(Comment comment, int userId)
        {
            return new FeedDTO
            {
                Type = ACTIVITY_FEED_TYPE.COMMENT,
                Date = comment.CreateDate,
                Comment = comment.Adapt(userId)
            };
        }

        public static FeedDTO SetLike(Like like, int userId)
        {
            return new FeedDTO
            {
                Type = ACTIVITY_FEED_TYPE.LIKE,
                Date = like.CreateDate,
                Like = like.Adapt(userId)
            };
        }
    }

这是我尝试查询数据的地方:

public IQueryable<FeedDTO> Feed(int userId)
        {

            var listComments =
                Context.Comments
                    /* includes ... */
                    .Select(x => FeedDTO.SetComment(x, userId));

            var listLikes =
                Context.Likes
                    /* includes ... */
                    .Select(x => FeedDTO.SetLike(x, userId));

            return listLikes.Union(listComments).OrderByDescending(x => x.Date);

}

问题是:

  • 当我单独归还每个人时,没有Union它可以正常工作
  • 当我尝试Union FeedDTO方法中的所有相关数据均为null

0 个答案:

没有答案