9

是否可以将 linq 查询的一部分传递给函数?我想为我的 DAL 创建一个始终使用相同查询接口的通用接口。例如,

List<T> Get(Join j, Where w, Select s){    
    return currentDataContext<T>.Join(j).Where(w).Select(s).ToList();    
}

这种事情可能吗?我认为这将使用表达式树来完成,但我无法找到它的示例。

4

5 回答 5

10

好吧,“加入”很棘手,因为很难表达加入 - 但是像 where / select / orderby 这样的事情很容易......

真的,这只是在 上组合各种 LINQ 方法的情况IQueryable<T>,这些方法通常接受Expression<Func<...>>某种组合。因此,带有可选谓词的基本选择将是:

    public IQueryable<T> Get<T>(
        Expression<Func<T,bool>> predicate
        ) where T : class
    {
        IQueryable<T> query = (IQueryable<T>)GetTable(typeof(T));
        if (predicate != null) query = query.Where(predicate);
        return query;
    }

我也倾向于返回IQueryable<T>,因为它是完全可组合的。如果调用者想要一个列表,他们总是可以使用ToList()它......或者(例如):

    using(var ctx = new MyDataContext(CONN))
    {
        ctx.Log = Console.Out;
        int frCount = ctx.Get<Customer>(c => c.Country == "France").Count();
    }

哪个(使用 Northwind)进行查询:

SELECT COUNT(*) AS [value]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[Country] = @p0

在查询中包含“选择”(投影)的问题是您最终会得到多个泛型类型。由于您经常希望投影是匿名类型,因此几乎不可能指定投影类型(匿名)表类型,并且它是不可调用的。

实际上,我想知道编写这样的方法是否有很多好处。我可能会坚持使用基本方法:

    public IQueryable<T> Get<T>() where T : class
    {
        return (IQueryable<T>)GetTable(typeof(T));
    }

并让调用者以他们喜欢的方式编写它——也许使用查询语法:

       var list = (from cust in ctx.Get<Customer>()
                   where cust.Country == "France"
                   select cust.CompanyName).Take(10).ToList();

哪个用途:

SELECT TOP (10) [t0].[CompanyName]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[Country] = @p0

或者,如果您确实想包含 order by 和 projection,那么扩展方法是最实用的方法;那么您不需要指定原始(源) T (这就是在与匿名类型混合时使其不可调用的原因):

public static class QueryExtension
{
    public static IQueryable<TProjection>
        Get<TSource, TProjection, TOrderKey>(
            this IQueryable<TSource> source,
            Expression<Func<TSource, bool>> where, // optional
            Expression<Func<TSource, TProjection>> select,
            Expression<Func<TProjection, TOrderKey>> orderBy)
    {
        if (where != null) source = source.Where(where);
        return source.Select(select).OrderBy(orderBy);
    }
}

然后考虑一个 DAL 方法,例如:

    public List<string> Countries()
    {
        return Customers.Get(
            x=>x.CompanyName != "",
            x=>x.Country,
            x=>x).Distinct().ToList();
    }

使用(再次,与罗斯文):

SELECT DISTINCT [t0].[Country]
FROM [dbo].[Customers] AS [t0]
WHERE [t0].[CompanyName] <> @p0
于 2009-01-12T06:25:51.603 回答
2

检查这个通用类:TableView.cs

它基本上使用 Func<TEntity, bool> 委托来应用 Where 谓词:

//...
public TableView(DataContext dataContext, Expression<Func<TEntity, bool>> predicate)
{
    this.table = dataContext.GetTable<TEntity>();
    this.baseQuery = table.Where(predicate);
    this.predicate = predicate.Compile();
}
//...
于 2009-01-12T05:25:08.090 回答
1

您可以使用 Linq 示例提供的动态表达式库,使用此扩展库,您可以为 where 等传递 linq 子句...

可从此处下载 linq 示例

于 2009-01-12T09:50:47.147 回答
0

您可以使用动态 LINQ并将参数作为字符串传递。

于 2009-01-12T05:22:24.647 回答
0

Marc Gravell♦ 像往常一样提供了一个非常有见地的答案,但我真的认为拥有采用IQueryables并添加限制的方法在大多数情况下都可以工作,并且它们使代码更清晰且易于维护。例如:

//Join
public static IQueryable<IContract> AllContracts(this IQueryable<IAccount> accounts, ISession s ) {
          return from a in accounts
                 from contract in s.Query<IContract()
                 where (a.Id == contract.AccountId)
                 select contract;
    }
//Where
public static IQueryable<IContract> Active(this IQueryable<IContract> contracts) {
          return from contract in contracts
                 where (contract.Active == true)
                 select contract;
    }

然后你可以像这样混合和匹配这些:

IQueryable<IContract> activeContracts = s.Query<IAccount>()
            .Where(o => o.Name == "XXX")
            .GetContracts(s)
            .Active();

我在这里使用扩展方法和 NHiberante 的 LINQ 提供Query方法,但这很容易在没有静态方法和任何 LINQ 提供程序的情况下重写。

于 2012-07-04T03:03:51.700 回答