在带有元组的linq连接中,LINQ to Entities中仅支持无参数构造函数和初始值设定项

Roc*_*cky 2 c# linq entity-framework tuples entity-framework-6

我试图加入两个表并创建一个元组列表,但得到的错误是"LINQ to Entities中只支持无参数构造函数和初始值设定项"

var list = (
    from a in db.CategoryRatings
    join c in db.OverallPerformanceByCategories
        on a.CategoryRatingId equals c.CategoryRatingId
    where c.ReviewId == review.ReviewId
    select(new Tuple<string, double, double>(a.CategoryRatingName, a.CategoryWeight, c.Score))
).ToList(); 

ViewData["ListOverallRating"] = list;
Run Code Online (Sandbox Code Playgroud)

我不想创建使用元组的匿名类型列表,也可以建议其他方式.

Mig*_*uel 7

使用匿名类型从数据库中获取并转换为内存中的元组.

var list = (
    from a in db.CategoryRatings
    join c in db.OverallPerformanceByCategories
        on a.CategoryRatingId equals c.CategoryRatingId
    where c.ReviewId == review.ReviewId
    select(new {a.CategoryRatingName, a.CategoryWeight, c.Score})
)
.AsEnumerable()
.Select(t =>
    new Tuple<string, double, double>(t.CategoryRatingName, t.CategoryWeight, t.Score))
.ToList(); 

ViewData["ListOverallRating"] = list;
Run Code Online (Sandbox Code Playgroud)

希望这有帮助!