为了获得更好的图片,我正在尝试使用不同元素组成的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