score:34
Accepted answer
In C#, something like:
var query = from category in mycatg
where category.IsPublic == 1
|| category.FirstName == "XXX"
group 1 by category.Catg into grouped
select new { Catg = grouped.Key,
Count = grouped.Count() };
The projection of "1" makes it clear that all we need is the key of the grouping and the count - the individual entries in each grouping are irrelevant.
Using lambda syntax and dot notation:
var query = mycatg.Where(category => category.IsPublic == 1
|| category.FirstName == "XXX")
.GroupBy(category => category.Catg,
category => 1)
.Select(grouped => new { Catg = grouped.Key,
Count = grouped.Count() });
Source: stackoverflow.com
Related Query
- How to combine Where clause and group by in LINQ
- How to combine select and group by clause with conditional where in Linq
- C# - Linq optimize code with List and Where clause
- How to combine LINQ group and ordering with parent and child tables?
- LINQ Group by and having where clause
- How to use LINQ group by with where clause
- How to AND predicates in the WHERE clause in a LINQ to RavenDB query
- Linq how to join tables with a where clause on each table and a count
- How to group by and calculate sum and select where is less than given amount in SQL Server / C# Linq
- How to convert sql to LINQ combine LEFT JOIN, WHERE, GROUP BY and SUM in LINQ?
- Linq - How to combine reflection with where and contains
- How do I use the having clause on an aggregate in a complex group by and join query in LINQ
- How to write OrderByDescending and where clause in single LinQ query
- How to query and calculate dates in the where clause of a LINQ statement?
- How to execute a linq query for each item in a list , use it in the where clause and return a collection from the result of each query?
- C# LINQ Query with group by, having and where clause invalid
- how to concert SQL query to LINQ with count , group by and isnull clause
- How to dynamically add OR operator to WHERE clause in LINQ
- linq where clause and count result in null exception
- linq styling, chaining where clause vs and operator
- How to combine 2different IQueryable/List/Collection with same base class? LINQ Union and Covariance issues
- Why C# LINQ expressions must end with Select or Group By Clause where as no such restriction in VB.Net
- How can I create a conditional where clause using LINQ
- When using a LINQ Where clause on a Dictionary, how can I return a dictionary of the same type?
- Linq where clause with multiple conditions and null check
- How to Use Effeciently Where Clause or Select in LINQ Parallel in Large Dataset
- How to add a where clause on a linq join (lambda)?
- How to use Linq and the IN clause
- How do I order the elements in a group by linq query, and pick the first?
- C# LINQ - How to build Group By clause dynamically
More Query from same tag
- How to simplify complicated conditional clauses when using LINQ to SQL
- MVC Linq .Random() wrong syntax
- Self Join or Inner Query in a table with Linq to SQL
- How do I use a left-join and non-left-join on more than 2 tables using linq
- ForEach -- where are you?
- LINQ + join with nested foreach razor output writing out title lines from groupby
- Convert a for loop to linq
- How to calculate sum for specific property without grouping main list data
- Change this Loop by LINQ?
- Workaround to use a method within a LINQ expression
- Is LINQ implementation of Entity Framework inappropriate?
- C# - using Linq to XML to populate list of objects
- Using LINQ to Entities dynamic JOIN
- Make ObservableCollection class with LINQ
- multiple linq joins with anonymous types using lambda syntax
- Pivot Table in LINQ in VB.NET
- Linq query where clause in sub list
- MongoDB .Net Linq GroupBy Child Array AsQueryable
- How to search for "Not Contains" condition on collection of objects in RavenDB
- Linq Query syntax System.Xml.Linq.XElement
- Using .Distinct() on certain rows
- How to add a linked sql server using LINQ in C#?
- Sort a list of lists by minimum value
- Where NavigationProperty.date >= Date, returns empty list even though database has valid data
- how to get the index of the widget and why is it giving only first value
- Registering all types in Assembly for Unity
- linQ of selecting all rows of an excel file accepting null values
- LINQ Composite Key GroupJoin with keys unknown at compile time
- How to leverage dynamic programming inside of a Linq Where clause?
- Filtering SQL Table results based upon C# collection