【问题标题】:Join multiple one to many related tables in EF and select as view model在 EF 中加入多个一对多相关表并选择作为视图模型
【发布时间】:2015-12-13 11:29:41
【问题描述】:

我的应用程序的数据库模型是:

public class Restaurant 
{
   public int Id { get; set; }
   .........
} 

public class Review
{
    public int Id { get; set; }
    public string ReviewTitle { get; set; }
    public string ReviewContent { get; set; }
    public int UserId { get; set; }
    public int RestaurantId { get; set; }
}

public ReviewHelpful
{
    public int Id { get; set; }
    public int ReviewId { get; set; }
    public bool IsHelpfull { get; set; }
}

public ReviewImage
{
   public int Id { get; set; }
   public string ImageLink { get; set; }
   public int ReviewId { get; set; }
}

任何表格中都没有导航属性。在ReviewHelpful 表中,如果用户发现此评论有帮助,则值为true,否则为false。 现在我想像这样创建一个视图模型:

public class ReviewViewModel 
{
       public int ReviewId { get; set; }
       public int RestaurantId { get; set; }
       public string ReviewTitle { get; set; }
       public string ReviewContent { get; set; }
       public int UserId { get; set; }
       public int NumberOfHelpfull { get; set; }
       public int NumberOfNotHelpfull { get; set; }
       public List<string> ImagesLinks { get; set; }
}

因此,我想编写这样的查询:

var reviews = (from review in _foodTrackerContext.RestaurantReviews
            join helpful in _foodTrackerContext.Helpfuls on review.Id equals helpful.ReviewId
            join reviewPicture in _foodTrackerContext.ReviewPictures on review.Id equals reviewPicture.ReviewId
            where review.ResturantId == 2
            select new ReviewViewModel()
            {
                Id = review.Id,
                RestaurantId = 2,
                ReviewTitle = review.ReviewTitle,
                ReviewContent = review.ReviewContent,
                NumberOfHelpfull = .. ??,
                NumberOfNotHelpfull = ... ??, 
                ImagesLinks = ... ???
            }

我无法使用此查询检索 HelpfulYesHelpfulNoImagesLinks。查找这些变量的查询是什么?。

此查询生成多行供单个审核,每个ReviewImage 和每个ReviewHelpful

【问题讨论】:

  • 还有什么问题?什么不工作?
  • 无法检索HelpfulYesHelpfulNoImagesLinks
  • 请再次检查您的问题。例如,添加缺少的 class 关键字。并使其保持一致:首先显示:public class ReviewViewModel ,然后像这样使用它:select new RestaurantReviewViewModel(),但名称不同。

标签: entity-framework sql-server-2008 join


【解决方案1】:

ypu需要做的查询是这个:

var model = 
    from review in ctx.Reviews
    where review.RestaurantId == 2
    join helpful in ctx.ReviewHelpfuls
        on review.Id equals helpful.ReviewId into helpfuls
    join image in ctx.ReviewImages
        on review.Id equals image.ReviewId into images
    select new RestaurantReviewViewModel
    {
        Id = review.Id,
        RestaurantId = 2,
        ReviewTitle = review.ReviewTitle,
        ReviewContent = review.ReviewContent,
        NumberOfHelpfull = helpfuls.Count(h => h.IsHelpfull),
        NumberOfNotHelpfull = helpfuls.Count(h => !h.IsHelpfull),
        ImagesLinks = (from image in images select image.ImageLink).ToList()
    };

请注意,当您进行一对多连接时,您需要包含一个into,以便为连接的实体命名以便能够对其进行处理。

我使用点语法来选择计数,但如果您愿意,可以使用查询语法。随着时间的推移,我发现 dot synatx 更加自然。

注意:如果您使用导航属性,这将变得更加容易。你为什么不使用它们?使用导航属性,您无需显式地进行连接,因为它们已经可用。

【讨论】:

  • 问题是,此查询为每个 imageLink 生成多行,有助于单次审查。我无法生成NumberOfHelpfulNumberOfNotHelpfulImageLinks 的计数。
  • ImagesLinks = (from image in images select image.ImageLink).ToList() 这行返回我LINQ to Entities does not recognize the method 'System.Collections.Generic.List1[System.String] ToList[String](System.Collections.Generic.IEnumerable 1[System.String]) method, and this method cannot be translated into a store expression. 这个错误。
  • 我不知道您以不同的方式在做什么,但我有一个概念证明,该代码完全可以工作。您显示的错误类型在您实现查询的地方没有任何意义。你一定写了一些不同的东西。
【解决方案2】:
List<ReviewViewModel> listModel = new List<ReviewViewModel>();


                 context.dbRestaurant
                                   .include("Review")
                                   .include("Review.ReviewHelpful")
                                   .include("Review.ReviewImage").ToList().ForEach((item) =>
            {
               ReviewViewModel model = new ReviewViewModel();
                  model.ID = item.ID
                listModel.Add(model);
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    相关资源
    最近更新 更多