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

c# entity-framework entity-framework-6 linq tuples

Question

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.

1
2
8/7/2018 8:15:09 PM

Accepted Answer

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!

8
8/7/2018 8:16:34 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow