计算LINQ中的多个列

时间:2018-02-01 15:22:46

标签: c# linq

我知道这个问题的最佳答案是更好和优化的数据库,但是可以在当前结构上优化这个总和吗?

表格是这样的:

rowid a1 b1 b2 b3
123   s  s  n  s
124   n  n  n  n
125   s  n  s  s

我需要计算所有" s"。

的数量
var items = AsDynamic(App.Data["items"]).Where(p => Convert.ToDateTime(p.data).Year == ano);

var items_1a = items.Where(u => u.udd == "_1a");

var items_1a_jan = items_1a.Where(m => Convert.ToDateTime(m.data).Month == 1);

int items_1a_jan_s = items_1a_jan.Count(q => q.a1 == "s") + items_1a_jan.Count(q => q.b1 == "s") + items_1a_jan.Count(q => q.b2 == "s") + items_1a_jan.Count(q => q.b3 == "s") + items_1a_jan.Count(q => q.b4 == "s") + items_1a_jan.Count(q => q.b5 == "s") + items_1a_jan.Count(q => q.b6 == "s") + items_1a_jan.Count(q => q.b7 == "s") + items_1a_jan.Count(q => q.b8 == "s") + items_1a_jan.Count(q => q.b9 == "s") + items_1a_jan.Count(q => q.b10 == "s") + items_1a_jan.Count(q => q.b11 == "s") + items_1a_jan.Count(q => q.b12 == "s") + items_1a_jan.Count(q => q.b13 == "s") + items_1a_jan.Count(q => q.c1 == "s") + items_1a_jan.Count(q => q.c2 == "s") + items_1a_jan.Count(q => q.c3 == "s") + items_1a_jan.Count(q => q.c4 == "s") + items_1a_jan.Count(q => q.c5 == "s") + items_1a_jan.Count(q => q.c6 == "s") + items_1a_jan.Count(q => q.c7 == "s") + items_1a_jan.Count(q => q.c8 == "s") + items_1a_jan.Count(q => q.c9 == "s") + items_1a_jan.Count(q => q.c10 == "s") + items_1a_jan.Count(q => q.c11 == "s") + items_1a_jan.Count(q => q.c12 == "s") + items_1a_jan.Count(q => q.c13 == "s") + items_1a_jan.Count(q => q.c14 == "s") + items_1a_jan.Count(q => q.c15 == "s") + items_1a_jan.Count(q => q.c16 == "s") + items_1a_jan.Count(q => q.c17 == "s") + items_1a_jan.Count(q => q.d1 == "s") + items_1a_jan.Count(q => q.d2 == "s") + items_1a_jan.Count(q => q.d3 == "s") + items_1a_jan.Count(q => q.d4 == "s") + items_1a_jan.Count(q => q.d5 == "s") + items_1a_jan.Count(q => q.d6 == "s") + items_1a_jan.Count(q => q.d7 == "s") + items_1a_jan.Count(q => q.d8 == "s") + items_1a_jan.Count(q => q.d9 == "s") + items_1a_jan.Count(q => q.e1 == "s") + items_1a_jan.Count(q => q.e2 == "s") + items_1a_jan.Count(q => q.e3 == "s") + items_1a_jan.Count(q => q.e4 == "s") + items_1a_jan.Count(q => q.e5 == "s") + items_1a_jan.Count(q => q.e6 == "s") + items_1a_jan.Count(q => q.e7 == "s") + items_1a_jan.Count(q => q.e8 == "s") + items_1a_jan.Count(q => q.e9 == "s") + items_1a_jan.Count(q => q.e10 == "s") + items_1a_jan.Count(q => q.e11 == "s");

3 个答案:

答案 0 :(得分:3)

使用SelectMany将每行的列展平为一个长列表:

var result = items.SelectMany(i=> new string[] {i.a1, i.b1, i.b2, i.b3 })
                  .Count(i => i == "s");

答案 1 :(得分:0)

如果你真的有这么多领域并且需要在所有领域中计算,那么你可以使用反射:

jq -r '["Date Found"], ["-------------"], (.Data[] | [.dateFound]) |  @tsv ' <<<$Report

答案 2 :(得分:0)

我不确定您如何存储数据,但您可以这样做:

MockHttpServletRequestBuilder