我已经研究了一段时间并尝试了几种不同的方法。我试图将类似项目(即StartWith 60或20)分组作为输出的一部分,但是当我在Groupby部分尝试StartsWith时它不起作用。
另外......不是在输出上使用60或20,有没有办法重命名为60 =供应商而20 =内部?
var query = referenceDt.AsEnumerable()
.Where(results => results.Field<Int32>("FaultCode").ToString().StartsWith("60") ||
results.Field<Int32>("FaultCode").ToString().StartsWith("20")
)
.GroupBy(results => new
{
//FaultCode = results.Field<int>("FaultCode")
FaultCode = results.Field<Int32>("FaultCode").ToString().StartsWith("60") ||
results.Field<Int32>("FaultCode").ToString().StartsWith("20")
})
.OrderBy(newFaultCodes => newFaultCodes.Key.FaultCode)
.Select(newFaultCodes => new
{
FaultCode = newFaultCodes.Key.FaultCode,
Count = newFaultCodes.Count()
})
;
答案 0 :(得分:0)
尝试类似:
.GroupBy(results => new
{
FaultCode = results.Field<Int32>("FaultCode").ToString().StartsWith("60") ? 60 : 20
})
和
.Select(newFaultCodes => new
{
FaultCode = newFaultCodes.Key.FaultCode.ToString().StartsWith("60")
? "Vendor" : "Internal",
Count = newFaultCodes.Count()
})
答案 1 :(得分:0)
您应该在某处拥有所有前缀的映射,并映射相应的值。您可以使用字典或与其他表连接。
如,
var faultTypes = new Dictionary<string, string>
{
["20"] = "Internal",
["60"] = "Vendor",
};
var query =
from r in referenceDt.AsEnumerable()
let faultCode = r.Field<int>("FaultCode")
let faultCodeStr = Convert.ToString(faultCode).Substring(0, 2)
where new[] { "20", "60" }.Contains(faultCodeStr)
group faultCode by faultTypes[faultCodeStr] into g
select new
{
FaultType = g.Key,
Count = g.Count(),
};