【问题标题】:how to fix the following issue in linq?如何解决linq中的以下问题?
【发布时间】:2013-12-09 11:38:32
【问题描述】:

我正在创建一个 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();

【问题讨论】:

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


    【解决方案1】:
    .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()
    

    【讨论】:

    • 它表示当前上下文中不存在全年。我已经更新了我的问题,请看一下..
    • 包含年份数据的列表是否在您的查询范围内可用?
    • 您想将新的 nonComplianceData 与带有查询的新列表关联,其中源是在新的 nonComplianceData 的构造函数中创建的列表?
    • 如果我正确理解您的问题,我编辑的答案应该可以解决问题。注意我输入这个没有检查小错别字等
    • 对不起不起作用...我已经按照您的建议更新了我的代码..您是这个意思吗?在代码中查看我的 cmets
    猜你喜欢
    • 2021-03-04
    • 2014-05-03
    • 2011-12-01
    • 2023-01-23
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多