Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data.Objects;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Text;
- namespace MRPRINT.COM.PL.WWW.Portal.DB.Key
- {
- public class KeyNumerLp
- {
- public KeyNumerLp()
- {
- Lp = 0;
- Typ = 0;
- Numer = 0;
- }
- public int Numer { get; set; }
- public short Typ { get; set; }
- public short Lp { get; set; }
- }
- public static class KeyNumberLpExt
- {
- public static IQueryable<TEntity> WhereKeyNumberLp<TEntity>(this ObjectQuery<TEntity> query,
- Expression<Func<TEntity, int>> selectorNumer,
- Expression<Func<TEntity, short>> selectorTyp,
- Expression<Func<TEntity, short>> selectorLp,
- IEnumerable<KeyNumerLp> collection)
- {
- if (selectorNumer == null || selectorTyp == null || selectorLp == null)
- throw new ArgumentNullException("selector");
- if (collection == null)
- throw new ArgumentNullException("collection");
- ParameterExpression parametrsNumber = selectorNumer.Parameters.Single();
- ParameterExpression parametrsTyp = selectorTyp.Parameters.Single();
- ParameterExpression parametrsLp = selectorLp.Parameters.Single();
- if (!collection.Any())
- return query;
- IEnumerable<Expression> equals = collection.Select
- (
- value =>
- {
- var exNumber = (Expression)Expression.Equal(selectorNumer.Body, Expression.Constant(value.Numer, typeof(int)));
- var exTyp = (Expression)Expression.Equal(selectorTyp.Body, Expression.Constant(value.Typ, typeof(short)));
- var exLp = (Expression)Expression.Equal(selectorLp.Body, Expression.Constant(value.Lp, typeof(short)));
- return (Expression)Expression.And(((Expression)Expression.And(exNumber, exTyp)), exLp);
- }
- );
- Expression body = equals.Aggregate((accumulate, equal) => Expression.Or(accumulate, equal));
- var lExpresion = Expression.Lambda<Func<TEntity, bool>>(body, parametrsNumber, parametrsTyp, parametrsLp);
- var retQuery = query.Where(lExpresion); ;
- return retQuery;
- }
- }
- }
- //---------------------------------------------------
- //EXECUTING
- List<KeyNumerLp> key = new List<KeyNumerLp>() { new KeyNumerLp() { Numer = 1, Lp = 1, Typ = 2 }, new KeyNumerLp() { Numer = 1, Lp = 1, Typ = 3 } };
- var query = ent.CDNXL_TwrKarty.WhereKeyNumberLp(
- t => t.Twr_GIDNumer,
- n => n.Twr_GIDTyp.Value,
- m => m.Twr_GIDLp.Value, key);
- var qsql = (ObjectQuery)query;
- string sql = qsql.ToTraceString();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement