4

我有一些非常相似的 LINQ 查询:

var city = "Hamburg";

var query1 = 
    from c in DB.Customers
    join address in DB.Addresses on c.ID equals address.CustomerID
    where address.City == city
    select c;

var query2 = 
    from c in DB.Customers
    join address in DB.Addresses on c.ID equals address.CustomerID
    where address.City.StartsWith(city)
    select c;

etc.

我想使用循环来创建查询以减少冗余:

var city = "Hamburg";

var expressions = new Expression<Func<string, bool>>[] {
    a => a == city,
    a => a.StartsWith(city)
};

for(int i = 1; i <= 2; i++) {
    queries.Add(
        from c in DB.Customers
        join address in DB.Addresses on c.ID equals address.CustomerID
        where expressions[i](address.City) 
        select c
        );
}

但我现在不知道如何创建表达式数组。任何想法?

4

4 回答 4

3

您可以Join直接使用该方法而不是使用查询语法:

public IQueryable<Customer> FindCustomers(Expression<Func<Customer, Address, bool>> predicate)
{
    return DB.Customers.Join(DB.Addresses, c => c.ID, a => d.CustomerID, (c, a) => new { Address = a, Customer = c})
        .Where(pair => predicate(pair.Address))
        .Select(pair => pair.Customer)
}

或者你可以同时通过Customerand Address

public IQueryable<Customer> FindCustomers(Expression<Func<Customer, Address, bool>> predicate)
{
    return DB.Customers.Join(DB.Addresses, c => c.ID, a => d.CustomerID, (c, a) => new { Address = a, Customer = c})
        .Where(pair => predicate(pair.Customer, pair.Address))
        .Select(pair => pair.Customer)
}

然后您可以将数组创建为:

IQueryable<Customer>[] queries = expressions.Select(expr => FindCustomers(expr)).ToArray();
于 2013-01-09T13:26:10.133 回答
3
var city = "Hamburg";

// predicate should accept Address
var expressions = new Expression<Func<Address, bool>>[] {
    a => a.City == city,
    a => a.City.StartsWith(city)
};

foreach(var predicate in expressions) {
    queries.Add(
        DB.Customers.Join(
           DB.Addresses.Where(predicate), // filtering here
           c => c.ID, 
           a => a.CustomerID, 
           (c, a) => c) // return customer
    ));
}
于 2013-01-09T13:31:57.470 回答
1

您可以在常用查询中保存常用连接部分。

var query = from c in DB.Customers
         join address from DB.Addresses on c.ID equals address.CustomerID
         select new {A=a, C=c};
var query1 = query.Where ( item => item.A.temp == item.C.test).Select (item => item.C);
var query2 = query.Where ( item => item.A.temp2 == item.C.test2).Select (item => item.C);
于 2013-01-09T13:25:50.453 回答
1
var queries=(
    from f in
        new Func<Address, bool>[] { 
            x => x.City==city, 
            x => x.City.StartsWith(city)
            }
    select
        from c in DB.Customers
        join address in DB.Addresses on c.ID equals address.CustomerID
        where f(address)
        select c
    ).ToArray();
  1. 您正在创建一个expression array,这意味着您expression array毕竟是在产生一个;你不需要让代表成为expression array.

  2. 你不需要foreach

  3. 如果您稍后将向 中添加更多表达式queries,则更ToArray()改为ToList()

  4. 如果您想Func<Address, bool>在生成之前进行修改queries,则为其定义一个局部变量,然后替换Func<Address, bool>[]

于 2013-01-09T13:58:05.893 回答