QueryExtensions IsBtwn

Απάντηση
Άβαταρ μέλους
GeoB
Δημοσιεύσεις: 11
Εγγραφή: Παρ Ιαν 21, 2022 10:09 pm

QueryExtensions IsBtwn

Δημοσίευση από GeoB »

public static IEnumerable<TSource> IsBtwn<TSource, TKey>(this IEnumerable<TSource> source, Expression<Func<TSource, TKey?>> field, TKey? frm, TKey? to) where TKey : struct, IComparable<TKey> => IsBtwn(source, field, frm, to, true);
public static IEnumerable<TSource> IsBtwn<TSource, TKey>(this IEnumerable<TSource> source, Expression<Func<TSource, TKey?>> field, TKey? frm, TKey? to, bool aplly) where TKey : struct, IComparable<TKey> {
if (!aplly) return source;
Expression fldEx = Expression.Invoke(field, field.Parameters);
var frmEx = Kexpression.GreaterThanOrEqualNullable(fldEx, Expression.Constant(frm));
var toEx = Kexpression.LessThanOrEqualNullable(fldEx, Expression.Constant(to));
Expression andEx = Expression.And(frmEx, toEx);
var lambda = Expression.Lambda<Func<TSource, bool>>(andEx, field.Parameters).Compile();
return source.Where(lambda);
}

public static IEnumerable<TSource> IsBtwn<TSource, TKey>(this IEnumerable<TSource> source, Expression<Func<TSource, TKey>> field, TKey frm, TKey to) where TKey : IComparable<TKey> => IsBtwn(source, field, frm, to, true);
public static IEnumerable<TSource> IsBtwn<TSource, TKey>(this IEnumerable<TSource> source, Expression<Func<TSource, TKey>> field, TKey frm, TKey to, bool aplly) where TKey : IComparable<TKey> {
if (!aplly) return source;
Expression fldEx = Expression.Invoke(field, field.Parameters);
Expression frmEx = Expression.GreaterThanOrEqual(fldEx, Expression.Constant(frm));
Expression toEx = Expression.LessThanOrEqual(fldEx, Expression.Constant(to));
Expression andEx = Expression.And(frmEx, toEx);
var lambda = Expression.Lambda<Func<TSource, bool>>(andEx, field.Parameters).Compile();
return source.Where(lambda);
}

public static IQueryable<TSource> IsBtwn<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey?>> field, TKey? frm, TKey? to) where TKey : struct, IComparable<TKey> => IsBtwn(source, field, frm, to, true);
public static IQueryable<TSource> IsBtwn<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey?>> field, TKey? frm, TKey? to, bool aplly) where TKey : struct, IComparable<TKey> {
if (!aplly) return source;
Expression fldEx = Expression.Invoke(field, field.Parameters);
var frmEx = Kexpression.GreaterThanOrEqualNullable(fldEx, Expression.Constant(frm));
var toEx = Kexpression.LessThanOrEqualNullable(fldEx, Expression.Constant(to));
Expression andEx = Expression.And(frmEx, toEx);
var lambda = Expression.Lambda<Func<TSource, bool>>(andEx, field.Parameters);
return source.Where(lambda);
}

public static IQueryable<TSource> IsBtwn<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> field, TKey frm, TKey to) where TKey : IComparable<TKey> => IsBtwn(source, field, frm, to, true);
public static IQueryable<TSource> IsBtwn<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> field, TKey frm, TKey to, bool aplly) where TKey : IComparable<TKey> {
if (!aplly) return source;
Expression fldEx = Expression.Invoke(field, field.Parameters);
Expression frmEx = Expression.GreaterThanOrEqual(fldEx, Expression.Constant(frm));
Expression toEx = Expression.LessThanOrEqual(fldEx, Expression.Constant(to));
Expression andEx = Expression.And(frmEx, toEx);
var lambda = Expression.Lambda<Func<TSource, bool>>(andEx, field.Parameters);
return source.Where(lambda);
}
Άβαταρ μέλους
cgoulas
Site Admin
Δημοσιεύσεις: 130
Εγγραφή: Παρ Ιαν 21, 2022 8:51 pm

Re: QueryExtensions IsBtwn

Δημοσίευση από cgoulas »

Μόνο καρδούλες!!! :lol: :lol:
Χρήστος Γούλας
.COM Business Computing
Άβαταρ μέλους
GeoB
Δημοσιεύσεις: 11
Εγγραφή: Παρ Ιαν 21, 2022 10:09 pm

Re: QueryExtensions IsBtwn

Δημοσίευση από GeoB »

Usage:

var q = from t in db.T.IsBtwn(x => x.Tdate, d1, d2)
join p in db.P on t.PID equals p.PID
where p.OID == oid
select t;


var q = from t in db.T.IsBtwn(x => x.Idx, 105, 605)
join p in db.P on t.PID equals p.PID
where p.OID == oid
select t;


var q = from t in db.T.IsBtwn(x => x.Idx, 105, 605)
join p in db.P.IsBtwn(x => x.Amt, 1000, 3000) on t.PID equals p.PID
where p.OID == oid
select t;
Απάντηση