Handy Linq Queries
Handy Linq Queries Tips
Tip: Count Parent & Child Records at once
Solution:
parent.Where(i => i.childId == 5).SelectMany(e => e.child).Count();
Tip: Selection of Multiple Fields from List or Database, and short it over a field or multiple fields. (note the last statement of the order by taking precedence over previous)
Solution:
var cat = db.categories .Select(cat => new { cat.Category, cat.Id }) .OrderBy(o => o.Id) .OrderByDescending(o => o.Category);
//.ThenByDescending(o => o.Category);
output
9 Utilities
3 Transportation
10 Shelter
2 Restaurants
11 Personal
15 Office exp
17 Miscellaneous
5 Medical
12 Kids schooling
6 Insurance
13 Household items
1 Grocery
4 Gifts
14 Fun money
8 Education
16 Donation
7 Clothing
Tip: Change all value for a list of items at once (without loop)
Solution:
_extendedExpenses.ToList().ForEach(e => e.IsChecked = isChecked);
Tip: GroupBy on Multiple Columns/Fields
Solution:
foreach (var stDepart in strataDepartments.GroupBy( x=> new { x.Name, x.Code })) { System.Diagnostics.Debug.WriteLine($"Name:{stDepart.Key.Name} Code:{stDepart.Key.Code}" + $" Count: {stDepart.Count()}"); }

Comments
Post a Comment