如何解决linq中的以下问题?

时间:2013-12-09 11:38:33

标签: c# .net linq asp.net-mvc-4 c#-4.0

我正在创建一个MVC 4应用程序。有一点我需要用一些数据填充模型。

我的模型如下,

public class NonComplianceData
{
    public NonComplianceData()
    {
        FullYearData = new List<MonthData>
        {
            new MonthData { Month = "January"  },
            new MonthData { Month = "February"  },
            new MonthData { Month = "March"  },
            new MonthData { Month = "April"  },
            new MonthData { Month = "May"  },
            new MonthData { Month = "June"  },
            new MonthData { Month = "July"  },
            new MonthData { Month = "August"  },
            new MonthData { Month = "September"  },
            new MonthData { Month = "October"  },
            new MonthData { Month = "November"  },
            new MonthData { Month = "December"  },
        };

    }

    public int InspectorId { get; set; }
    public string InspectorName { get; set; }
    public IEnumerable<MonthData> FullYearData { get; set; }
}

public class MonthData
{
    public string Month { get; set; }
    public int TotalAuditsCompleted { get; set; }
    public int TotalNoDefects { get; set; }
    public float NonComplianceRate { get; set; }
}

要获取数据,我已经走了这么远......

var InspectorData = context.COESDetails
    .Where(x => 
        x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && 
        x.UploadCOESDetails.AuditMonth.Contains(criteria.AuditYear))            
    .Select(x => x.Inspector)
    .Where(y=>y.Id != 0)
    .Distinct()
    .OrderBy(x => x.Firstname)
    .Select(ud => new NonComplianceData
    {
        InspectorId = ud.Id,
        InspectorName = ud.Firstname + " " + ud.Surname,
        FullYearData =  ???????
    });

我遇到的问题是FullYearData。我需要一种方法来遍历fullYeardata,每月选择并进行更多查询。我可以通过linq做到这一点吗?

通过执行以下操作,我设法得到了我需要的东西。它的一些foreach循环..这可以通过linq完成吗?我在哪里设置InspectorName等?

var InspectorData = 
    context.COESDetails.Where(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth.Contains(criteria.AuditYear)).Select(x => x.Inspector).Where(y => y.Id != 0).Distinct().OrderBy(x => x.Firstname)
        .Select(ud => new NonComplianceData
            {
                InspectorId = ud.Id,
                InspectorName = ud.Firstname + " " + ud.Surname
            }).ToList();

foreach (var Inspector in InspectorData)
{
    foreach (var MonthData in Inspector.FullYearData)
    {
        var auditMonthYear = MonthData.Month + " " + criteria.AuditYear;
        MonthData.TotalAuditsCompleted = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected);
        MonthData.TotalNoDefects = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected && x.COESDetailsCOESDefects.Any());
    }
}

任何帮助都会得到帮助

由于

MiddlePat - 这是你的意思吗?这不起作用

var InspectorData = context.COESDetails.Where(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth.Contains(criteria.AuditYear)).Select(x => x.Inspector).Where(y => y.Id != 0).Distinct().OrderBy(x => x.Firstname) 
                    .Select(ud =>
                        new NonComplianceData
                        {
                            InspectorId = ud.Id,
                            InspectorName = ud.Firstname + " " + ud.Surname,
FullYearData = Inspector.FullYearData.Select(fyd => {
MonthData monthData = new MonthData();
var auditMonthYear = MonthData.Month + " " + criteria.AuditYear;
monthData.TotalAuditsCompleted = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected);
monthData.TotalNoDefects = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected && x.COESDetailsCOESDefects.Any());

return monthData;}).ToList()
                        }).ToList();

1 个答案:

答案 0 :(得分:2)

.Select(ud => new NonComplianceData
        {
            InspectorId = ud.Id,
            InspectorName = ud.Firstname + " " + ud.Surname,
            FullYearData =  FullYearData.Select(fyd => fyd.Month) //results in an IEnumerable<String>
       });

您可以继续在linqqueries中创建子查询。您还可以在所选列表上应用其他查询。

例如:

FullYearData = Inspector.FullYearData.Select(fyd => {
MonthData monthData = new MonthData();
var auditMonthYear = MonthData.Month + " " + criteria.AuditYear;
monthData.TotalAuditsCompleted = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected);
monthData.TotalNoDefects = context.COESDetails.Count(x => x.UploadCOESDetails.AuditZoneId == criteria.AuditZoneId && x.UploadCOESDetails.AuditMonth == auditMonthYear && x.InspectorId == Inspector.InspectorId && x.AuditType != (int)AuditType.NotSelected && x.COESDetailsCOESDefects.Any());

return monthData;}).ToList()