Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only parameterless constructors and initializers are supported in LINQ to Entities in linq join with tuples [duplicate]

I am trying to join two table and create a tuples list but getting error as 'Only parameterless constructors and initializers are supported in 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;

I don't want to create an anonymous type list to using tuples, can suggest other way as well.

like image 267
Rocky Avatar asked Oct 19 '25 01:10

Rocky


1 Answers

Use the anonymous type to fetch from the database and convert to a Tuple in memory.

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;

Hope this help!

like image 77
Miguel Avatar answered Oct 21 '25 13:10

Miguel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!