繁体   English   中英

如何在Asp.net MVC中将Linq Join与ViewModel绑定?

[英]How To Bind Linq Join With ViewModel In Asp.net MVC?

在这里,我试图通过使用LINQ联接两个表来获取列表,并且希望从ContestantRatings表中获取ContestantId。 但是它在ContestantId = Convert.ToInt32(CR.ContestantId)行中显示错误,该错误说:“ LINQ to Entities无法识别Int32 ToInt32(System.Object)方法,并且该方法无法转换为商店表达式。”
任何帮助将不胜感激。

下面是我的代码

public List<ContestantRating.Core.ViewModel.ContestantRatingVM> GetContestantRatingList()
{
    var contestantRatingList = (from C in db.Contestants where C.IsActive==true
                                join CR in db.ContestantRatings on C.Id equals CR.ContestantId 
                                select new ContestantRatingVM
                                {
                                    ContestantId  = Convert.ToInt32(CR.ContestantId),
                                    FirstName = C.FirstName,
                                    LastName = C.LastName,
                                    DateOfBirth = C.DateOfBirth,
                                    District = C.District.DistrictName,
                                    Rating = CR.Rating,
                                    RatingId = CR.Id,
                                    RatedDate=CR.RatedDate
                                }).ToList().OrderByDescending(x => x.RatingId);
    return contestantRatingList.ToList();
}


下面是我使用的视图模型ContestantRatingVM

public class ContestantRatingVM
{
    public int  ContestantId { get; set; }
    public Nullable<int> HomeZoneId { get; set; }
    public string District { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Nullable<System.DateTime> DateOfBirth { get; set; }
    public Nullable<bool> IsActive { get; set; }
    public string Gender { get; set; }
    public string PhotoUrl { get; set; }
    public string Address { get; set; }
    public int RatingId { get; set; }
    public Nullable<decimal> Rating { get; set; }
    public Nullable<System.DateTime> RatedDate { get; set; }
}

问题是对Convert.ToInt32的调用。 将Linq转换为实体会将Linq查询转换为数据库查询,因此支持的操作集有限。 为了解决这个问题,您有几种选择。

一种选择是在数据库查询之后进行转换:

public List<ContestantRating.Core.ViewModel.ContestantRatingVM> GetContestantRatingList()
{
  var tempArray = (from c in db.Contestants where C.IsActive==true
                   join cr in db.ContestantRatings on C.Id equals CR.ContestantId 
                   select new { C = c, CR = cr }).ToArray();
  var contestantRatingList = from x in tempArray select new ContestantRatingVM
                                {
                                    ContestantId  = Convert.ToInt32(x.CR.ContestantId),
                                    FirstName = x.C.FirstName,
                                    // ...
                                }).ToList().OrderByDescending(x => x.RatingId);
  return contestantRatingList.ToList();
}

通过调用ToArray ,您可以将查询发送到数据库,从而使Linq to Enities不必处理Convert.ToInt32

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM