给定一个列表IQueryables,如何在不在数据库中执行多个语句的情况下对每个列表进行求和?
return queries
.Sum(qy=> qy.Count());
上述工作正常,但会针对每个查询点击数据库。
答案 0 :(得分:4)
您可以先使用Aggregate
功能与Concat
合并IQueryable,然后Count
总计如下:
return queries.Aggregate((x,y) => x.Concat(y)).Count()
答案 1 :(得分:2)
从这个想法开始Sum(q1,q2) = q1.Concat(q2).Count()
我测试了以下扩展程序:
public static class LinqExtensions
{
public static IQueryable<object> ConcatAny<T,R>(this IQueryable<T> q1, IQueryable<R> q2)
{
return q1.Select(c=>(object)null).Concat(q2.Select(c=>(object)null));
}
public static IQueryable<object> ConcatAll(this IEnumerable<IQueryable<object>> queries)
{
var resultQuery = queries.First();
foreach (var query in queries.Skip(1))
{
resultQuery = resultQuery.ConcatAny(query);
}
return resultQuery;
}
}
我假设您有异构查询,例如IQueryable<T>
,IQueryable<R>
等等,您有兴趣计算所有行,无论其来源是什么。
所以你可以使用像
这样的扩展名var q1 = Table1.AsQueryable();
var q2 = Table2.AsQueryable();
var q3 = Table3.AsQueryable();
var queries = new IQueryable<object>[] {q1,q2,q3}; // we use here the covariance feature
return queries.ConcatAll().Count();
生成的SQL可能如下所示
SELECT COUNT(*) AS [value]
FROM (
SELECT NULL AS [EMPTY]
FROM (
SELECT NULL AS [EMPTY]
FROM [Table1] AS [t0]
UNION ALL
SELECT NULL AS [EMPTY]
FROM [Table2] AS [t1]
) AS [t2]
UNION ALL
SELECT NULL AS [EMPTY]
FROM [Table3] AS [t3]
) AS [t4]
我认为虽然
非常有效答案 2 :(得分:1)
如果您使用的是Entity Framework,则可以使用名为EntityFramework.Extended的扩展程序。有一个名为Future Queries
的内置扩展程序。这将允许您指定在下次进行数据库访问时应执行查询。
NuGet命令:
Install-Package EntityFramework.Extended
示例代码:
static void Main(string[] args)
{
using (var context = new MyDbContext())
{
var modelSet1 = context.Models.Where(x => x.ModelId < 25).FutureCount();
var modelSet2 = context.Models.Where(x => x.ModelId > 25 && x.ModelId < 32).FutureCount();
var modelSet3 = context.Models.Where(x => x.ModelId > 32).FutureCount();
var queries = new [] {modelSet1, modelSet2, modelSet3};
var countQueries = queries.Sum(x => x.Value);
Console.WriteLine(countQueries);
}
Console.ReadLine();
}
答案 3 :(得分:1)
好的,迟到几分钟,但我明白了! 这是代码:
public static class LinqExtensions
{
public static int CountAll(this IEnumerable<IQueryable<object>> queries)
{
if (queries == null || !queries.Any())
{
throw new ArgumentException("Queries parameter cannot be null or empty");
}
Expression ex = Expression.Constant(0);
foreach (var qy in queries)
{
// create count expression
var expression = Expression.Call(
typeof(Queryable),
"Count",
new[] { qy.ElementType },
qy.Expression
);
ex = Expression.Add(ex, expression);
}
return queries.First().Provider.Execute<int>(ex);
}
}
您将其用作queries.CountAll()
,其中查询为IEnumerable<IQueryable<object>>
,与Adrian的答案一样,甚至是简单的IEnumerable<IQueryable>
。
以下是分析器的SQL结果示例:
exec sp_executesql N'SELECT @p0 + ((
SELECT COUNT(*)
FROM [A] AS [t0]
WHERE [t0].[i1] >= @p1
)) + ((
SELECT COUNT(*)
FROM [B] AS [t1]
WHERE [t1].[i2] >= @p2
)) + ((
SELECT COUNT(*)
FROM [C] AS [t2]
WHERE [t2].[i3] >= @p3
)) AS [value]',N'@p0 int,@p1 int,@p2 int,@p3 int',@p0=0,@p1=2,@p2=2,@p3=2
的代表
var a = db.GetTable<A>();
var b = db.GetTable<B>();
var c = db.GetTable<C>();
var q1 = a.Where(v => v.i1 >= 2);
var q2 = b.Where(v => v.i2 >= 2);
var q3 = c.Where(v => v.i3 >= 2);
var queries = new IQueryable<object>[] {
q1,q2,q3
};
请注意,A,B和C是不同的对象/表,具有不同数量的属性/列,并且表达式是随机的过滤器。