string dbPath = Path.GetFullPath(Path.Combine(Application.StartupPath, @"..\..\..\..\Data\NORTHWND.MDF"));
string sqlServerInstance = @".\SQLEXPRESS";
string connString = "AttachDBFileName='" +
dbPath + "';Server='" +
sqlServerInstance + "';user instance=true;Integrated Security=SSPI;Connection Timeout=60";
Northwind db = new Northwind(connString);
db.Log = Console.Out;
var query =
db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10).
OrderBy("CompanyName").
Select("New(CompanyName as Name, Phone)");
Console.WriteLine(query);
Console.ReadLine();
public static IQueryable Where(this IQueryable source, string predicate, params object[] values) {
if (source == null) throw new ArgumentNullException("source");
if (predicate == null) throw new ArgumentNullException("predicate");
LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
return source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Where",
new Type[] { source.ElementType },
source.Expression, Expression.Quote(lambda)));
}
public static LambdaExpression ParseLambda(ParameterExpression[] parameters, Type resultType,
string expression, params object[] values)
{
ExpressionParser parser = new ExpressionParser(parameters, expression, values);
return Expression.Lambda(parser.Parse(resultType), parameters);
public class ContratENT
public int Periodicite {get; set;}
public decimal Versement {get; set;}
public decimal Taux {get; set;}
public string Police { get; set; }
//Initialisation simulée d'un ensemble de 8000 contrats
List<ContratENT> c = new List<ContratENT>();
int cpt = 0;
do
tabSource[cpt] = cpt1;
c.Add(new ContratENT { Police = "num" + cpt.ToString(), Taux = 5.5M, Periodicite = 12, Versement = cpt * 10 });
while (cpt++ < 7999);
int montantMin = 100;
var req = c.AsQueryable()
.Where("Versement >= @0", montantMin)
.OrderBy("Versement desc")
.Select("new(police, Taux, Periodicite,Versement, (Versement * Periodicite) * (Taux / 10) as Volume )");
dataGridView1.DataSource = req.Cast<object>().ToList();
public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> source, string ordering, params object[] values)
if (ordering == null) throw new ArgumentNullException("ordering");
return OrderBy(source.AsQueryable(), ordering, values).AsEnumerable();
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, string predicate, params object[] values)
return Where(source.AsQueryable(), predicate, values).AsEnumerable();
public static IEnumerable<Object> Select<T>(this IEnumerable<T> source, string selector, params object[] values)
if (selector == null) throw new ArgumentNullException("selector");
return Select((IQueryable)source.AsQueryable(), selector, values).Cast<Object>();