score:2
Accepted answer
Conditional Count
SQL translation was not supported well in EF6 and at all in EF Core until now.
EF Core 5.0 (currently in preview) finally adds correct translation, so the sample code works without modifications.
In pre EFC Core 5.0 you could use the equivalent conditional Sum
, i.e. instead of
Count(condition)
use
Sum(condition ? 1 : 0)
In your query
TrueVotes = x.Sum(v => v.Vote == true ? 1 : 0),
FalseVotes = x.Sum(v => v.Vote == false ? 1 : 0),
Unrelated, but you should probably exclude Vote
from grouping key
//.GroupBy(x => new { t.Type, t.Vote })
.GroupBy(x => new { t.Type })
otherwise counts will always be 0 and 1 or vice versa.
Source: stackoverflow.com
Related Query
- GroupBy and Count by condition Entity Framework
- Entity Framework Group By with Max Date and count
- Difference between && and where condition in entity framework query
- How to query many-to-many relationship with 'AND' condition using LINQ and Entity Framework
- Entity Framework Linq, Left Join and group with SUM and Count
- How to get a count of records with entity framework and repository pattern
- SQL subquery result in LINQ and Entity Framework Code First
- Entity Framework - groupby and timespan calculation
- Get value of dictionary element and used in where condition in Entity Framework
- How do I set a parameter based on a condition using LINQ and Entity framework
- Entity Framework Code First override onModelCreating() for TPT Inheritance Screwing Up Identity User and Role Models
- GroupBy returning Count and Value on condition
- GroupBy and Count with condition using LINQ
- LINQ Entity Framework - COUNT AND QUERY
- how to execute a nested group aggregate functions with multi groupBy columns using entity framework and linq?
- Get Count of Lazy Loaded Child Collection Using LINQ and Entity Framework
- Entity Framework LINQ query to return a list and subset of collection satisfying certain condition
- Entity Framework 4 and Linq to Entities specifications: How to code it?
- How to use groupby clause in Entity Framework and ASP.NET MVC 4?
- Join 2 tables to retrieve name and count linq Entity Framework
- Using group in Entity Framework to return model and count
- Entity Framework | GroupBy and Sum
- Entity Framework Select with Include + Group By Count + Paging and Projection
- Entity Framework Exclude Fields Query Count and POCO best way
- Entity Framework / LINQ Round and Count Latitude/Longitude
- LINQ with groupby and count
- Difference between Select and Where in Entity Framework
- Entity Framework, Code First and Full Text Search
- What are the Java equivalents to Linq and Entity Framework
- Join and Include in Entity Framework
More Query from same tag
- BindableLinq collection nesting
- LINQ to SQL Add CASE STATEMENT then original field NOT FOUND
- How to process a list of strings by vowel using LINQ?
- C# - datagridview doesn't show query result
- LINQBridge users: is it feature-complete?
- Linq join without adding a layer
- Filtering a Collection using Linq based on Combo boxes Selections
- Lambda overload for Skip/Take Missing
- LINQ to entities - Building where clauses to test collections within a many to many relationship
- Where clause in LINQ query using a function
- Pick the first item in a Linq to Entities query
- EF Core difficulty with INNER JOINS and LEFT JOINS
- Filter records in database according to the selected checkboxes using MVC
- Linq To XML, yield and others
- Get Average of column using group by
- Linq query result into two objects
- Converting LINQ query syntax to Lambda notation manually
- Linq to Entities - Delete / Add related data (many to many relationship)
- ASP.NET MVC how to create a model which pulls in data from 2 tables, to display in 1 view
- LINQ: convert .Single() to query notation
- EF Core ThenInclude foreign key values with 1:n relationshop
- Most efficient way to check to see if there is any data in a SQL row
- Why is DynamicMethod so much slower on x64?
- LINQ Query to create Dictionary of Dictionaries
- How to select only specific properties of navigation property
- Easy way to select more than one field using LINQ
- Search in the List Item Using C#
- need a simple linq
- Get list of all items of a generic type from list which could contain other types
- How can i get a single response using linq