Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using EfCoreOverview_Demo.Entities;
- using System;
- using System.Linq;
- using System.Linq.Expressions;
- namespace EfCoreOverview_Demo
- {
- class Program
- {
- static void Main(string[] args)
- {
- var context = new TelerikAcademyContext();
- var minSalaryGroups = context.Employees
- .GroupBy(e => e.DepartmentId, (key, group) => new
- {
- DepartmentId = key,
- MinSalary = group.Min(e => e.Salary)
- })
- .ToList();
- var predicate = minSalaryGroups.Aggregate(
- ToExpression((Employees x) => false),
- (expr, next) => expr.Or(e => e.DepartmentId == next.DepartmentId &&
- e.Salary == next.MinSalary)
- );
- var minSalaryEmployees = context.Employees
- .Where(predicate)
- .Select(e => new
- {
- e.FirstName,
- e.LastName,
- e.Salary,
- })
- .ToList();
- }
- public static Expression<Func<T, R>> ToExpression<T, R>(Expression<Func<T, R>> e) => e;
- }
- public static class ExpressionExtensions
- {
- public static Expression<Func<T, bool>> Or<T>(
- this Expression<Func<T, bool>> left,
- Expression<Func<T, bool>> right)
- => Expression.Lambda<Func<T, bool>>(
- Expression.OrElse(
- left.Body,
- Expression.Invoke(right,left.Parameters.Cast<Expression>())),
- left.Parameters);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement