LINQ将匿名列表转换为强类型,并在组BY中使用日期拆分

时间:2013-12-17 08:19:30

标签: linq

我正在尝试将日常日志组合在一起,从dbset中查找linq查询,达到或低于允许使用百分比。

dbset已开启

public partial class DailyUsageForYear
{
    public System.DateTime DateAdded { get; set; }
    public Nullable<long> NumUsers { get; set; }
    public int AllowedUsageCount { get; set; }
    public string Application { get; set; }
    public System.Guid ApplicationID { get; set; }
    public System.Guid SupplierID { get; set; }
}

我正在尝试获取像

这样的对象列表
public class UsageDisplay
{
    public int Year { get; set; }
    public int Month { get; set; }
    public string Application { get; set; }
    public int NumUsers { get; set; }
    public int AllowedUsageCount { get; set; }
    public Guid ApplicationID { get; set; }
}

然而,我的陈述末尾的选择似乎并不理解字段名称,有人可以告诉我,我对linq语法缺少什么?

var predicate = PredicateBuilder.False<DailyUsageForYear>();
    predicate = predicate.And (x => x.NumUsers > (x.AllowedUsageCount *    (DropPercentage/100)));
if (supplier != null)
{
    Guid supplierGuid = new Guid (supplier.ToString());
    predicate = predicate.And (x => x.SupplierID == supplierGuid);
}
if (Direction == 0)
    predicate = predicate.And (x => x.NumUsers < (x.AllowedUsageCount * (Percentage/100)));
else
    predicate = predicate.And (x => x.NumUsers > (x.AllowedUsageCount * (Percentage/100)));
usageDetailModel.usage = db.DailyUsageForYears.**Where**(predicate)
    .**GroupBy**(x => new { x.DateAdded.Year, x.DateAdded.Month, x.Application, x.NumUsers, x.SeatCount, x.LicenceID })
    .**Select**(u => new UsageDisplay() { Year = u.DateAdded.Year, Month = u.DateAdded.Month, Application = u.Application, NumUsers = u.NumUsers, SeatCount = u.SeatCount, ApplicationId = u.ApplicationID });

1 个答案:

答案 0 :(得分:0)

最后一次选择u内部DailyUsageForYear不是Key.Select(u => new UsageDisplay() { Year = u.Key.Year, Month = u.Key.Month, Application = u.Key.Application, NumUsers = u.Key.NumUsers, SeatCount = u.Key.SeatCount, ApplicationId = u.Key.ApplicationID }); 字段的批处理对象,因此请尝试更改您的选择

.GroupBy(x => new { 
        x.DateAdded.Year, 
        x.DateAdded.Month, 
        x.Application,
        x.NumUsers, 
        x.SeatCount, 
        x.LicenceID })

也在

x.ApplicationId

我认为您需要x.LicenceID而不是{{1}}