score:2
Accepted answer
The Count
function just:
Returns the number of elements in a sequence.
What you want to do is count the number of items in the sequence that match a certain condition. For that use the override the accepts a predicate:
.Select(g => new AppVersionModel {
DateYYMMDD = g.Key,
Android043 = g.Count(i => i.Android043 == 1),
Android044 = g.Count(i => i.Android044 == 1),
// ....
})
As you made sure the properties have 0,1 values you could also just use Sum
:
.Select(g => new AppVersionModel {
DateYYMMDD = g.Key,
Android043 = g.Sum(i => i.Android043),
Android044 = g.Sum(i => i.Android044),
// ....
})
However I'd go with a different approach of having the properties store booleans as seen below. In addition no need to write x.BOOLEAN_PROPERTY == true
, just write x.BOOLEAN_PROPERTY
.
.Select(x => new AppVersionModel {
DateYYMMDD = x.DateYYMMDD,
Android043 = x.IsAndroid && x.AppVersion == "0.4.3",
Android044 = x.IsAndroid && x.AppVersion == "0.4.4",
Android050 = x.IsAndroid && x.AppVersion == "0.5.0",
IOS043 = x.IsIos && x.AppVersion == "0.4.3",
IOS044 = x.IsIos && x.AppVersion == "0.4.4",
IOS050 = x.IsIos && x.AppVersion == "0.5.0",
})
.GroupBy(x => x.DateYYMMDD)
.OrderBy(g => g.Key)
.Select(g => new AppVersionModel {
DateYYMMDD = g.Key,
Android043 = g.Count(i => i.Android043),
Android044 = g.Count(i => i.Android44),
// ...
})
Source: stackoverflow.com
Related Articles
- How can I flatten out data in a list and then get a count of what's in each column using LINQ?
- c# Linq or code to extract groups from a single list of source data
- How to get linq `ForEach` statement to return data on the method call being made for each list object?
- c# Group a list and get the count of each group
- Is a full list returned first and then filtered when using linq to sql to filter data from a database or just the filtered list?
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
- How to group by list of values and then count the amount of entries per value
- How to count the occurence of each number in list A that is exist in list B and return zero if not existed?
- How get count of each distinct item in generic List in C#
- Select common elements for 2 or more departments from list group by then having count
- Grouping list of data with LINQ and get maximum from each grouped data
- I want to count number of each element in list using linq in c#
- List or Array of String Contain specific word in Html Source Code
- List that groups by property and then counts distinct values of another property and writes count to list property
- C# LINQ group collection by attribute then sort each group by explicit order defined by a list
- Is there a way I can get the count of all entries of data created by their day of week for each week
- Get 50 employees data from each department list
- Query data in certain period of time, and group by each date and count total data for each date
- How can I find the entries with dates older then a specified date in a list but all data types are string?
- LINQ: Join 5 or 6 tables and group by one then count groupby data
- Flatten List in LINQ
- how to remove empty strings from list, then remove duplicate values from a list
- How to Count Duplicates in List with LINQ
- The data source does not support server-side data paging
- Convert CollectionBase to List or data type usable with Linq
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Is it possible to use Linq to get a total count of items in a list of lists?
- Flatten List<string[]> into single string with one line for each element
- Get a list of distinct items and their count
- Exclude items of one list in another with different object data types, LINQ?
- Linq query to fill complex model?
- Custom (derived) List<T>
- Replace a part of an element in a list of strings
- Query on a many to one and then returning the parents
- Linq to XML - Read/Parse only first lines of the file
- LINQ query to get outstanding invoices
- Using an list in a query in entity framework
- Sorting XML using LINQ
- Looping through DataTable & Constructing an Xml for each unique item
- How Can I Match Orders to Products?
- Strange T-SQL / LINQ Performance issue when paging
- Linq to CSV select by column value
- Entity framework evaluating clause even after getting false
- Assignment Error in LINQ
- Gets exceptions ""at least one object must implement icomparable"" only when apply method ToList() to IQueryable<>
- Get the whole string from a String array if the string contains a substring
- How to avoid repeating ToLists when using LINQ and Entity Framework?
- in foreach loop, should I expect an error since query collection has changed?
- The parameter 'd' was not bound in the specified LINQ to Entities query expression
- Multiple Joins LINQ Query performance