使用连接但不同条件的相同LINQ查询

时间:2013-01-09 13:19:18

标签: c# linq

我有一些非常相似的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 个答案:

答案 0 :(得分: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)
}

或者您可以同时传递CustomerAddress

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();

答案 1 :(得分: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
    ));
}

答案 2 :(得分: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);

答案 3 :(得分: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>[]

    < / LI>