实体框架检查嵌套的空对象

时间:2013-06-22 12:39:29

标签: linq frameworks entity

我正在使用实体框架LINQ来显示部门,处理和检查信息。每个部门都有多个流程,每个流程都可以进行多次检查。我希望以一种方式来处理数据,即部门将仅针对多个流程显示一次,并且每个流程检查都应显示在流程前面。

以下是示例。


| Deptarment |过程|检查|

|部门1 |过程A |检查A

| ------------ | ------------- |检查B

|部门2 |过程B |检查A

| ------------ | ------------- |检查B

| ------------ | ------------- |检查C

| ------------ | ------------- |检查D

|部门3 |过程A |检查A

| ------------ | ------------- |检查A

|部门4 |过程A |第4部分不应该在我的案例中显示

| ------------ | ------------- |因为它没有任何检查信息。

    [WebMethod]
// [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public string GetDataEntryList(int companyId)
{
    VisioneEntities vEntity = new VisioneEntities();
    var retValue =
                (from dept in vEntity.Departments
                 join prooo in vEntity.Processes on dept.DepartmentId equals prooo.DepartmentId
                 select new
                 {
                     DepartmentId = dept.DepartmentId,
                     DepartmentName = dept.DepartmentName,
                     DataEntryProcessInfoView = (from pro in vEntity.Processes
                                                 where pro.DepartmentId == dept.DepartmentId
                                                 select new DataEntryProcessInfo()
                                                 {
                                                     ProcessId = pro.ProcessID,
                                                     ProcessName = pro.ProcessName,
                                                     DataEntryCheckInfos = from chk in vEntity.ProcessChecks
                                                                           where chk.ProcessId == prooo.ProcessID
                                                                           select new DataEntryCheckInfo() { CheckId = chk.CheckId, CheckName = chk.CheckName, Frequency = chk.Frequency }
                                                 }
                                    )
                 }).AsEnumerable().Select(x => new DataEntryInfoView() { DepartmentId = x.DepartmentId, DepartmentName = x.DepartmentName, DataEntryProcessInfos = x.DataEntryProcessInfoView });

    //var filterItems=from ret in retValue
    //                select new {r1=ret.DepartmentId,r2=ret.DataEntryProcessInfos.Select

    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var json = serializer.Serialize(retValue.AsEnumerable());

    return json;


}

以下是我使用的课程。

    public class DataEntryInfoView
    {

    public int DepartmentId { get; set; }
    public string DepartmentName { get; set; }
    public DataEntryCheckInfo checkInfo;
    public IQueryable<DataEntryProcessInfo> DataEntryProcessInfos;
    }

    public class DataEntryCheckInfo
    {
    public int CheckId { get; set; }
    public string CheckName { get; set; }
    public string Frequency { get; set; }
    }

    public class DataEntryProcessInfo
    {
    public int ProcessId { get; set; }
    public string ProcessName { get; set; }
    public IQueryable<DataEntryCheckInfo> DataEntryCheckInfos; 
    }

这是我不希望dipslay那些没有DataEntryCheckInfo为空的记录的结果

以下是我得到的结果

    {"d":"[{\"DataEntryProcessInfos\":[{\"DataEntryCheckInfos\":[{\"CheckId\":21,\"CheckName\":\"Fahad     \",\"Frequency\":\"Weekly\"},{\"CheckId\":22,\"CheckName\":\"Fahad     \",\"Frequency\":\"Weekly\"},{\"CheckId\":63,\"CheckName\":\"Check Test\",\"Frequency\":\"Fortnightly\"}],\"ProcessId\":7,\"ProcessName\":\"Process 1\"}],\"DepartmentId\":2,\"DepartmentName\":\"Dept 2\"},{\"DataEntryProcessInfos\":[{\"DataEntryCheckInfos\":[{\"CheckId\":86,\"CheckName\":\"afdassd   \",\"Frequency\":\"Yearly\"}],\"ProcessId\":8,\"ProcessName\":\"Wasqur Process\"}],\"DepartmentId\":7,\"DepartmentName\":\"Dept 6\"},{\"DataEntryProcessInfos\":[{\"DataEntryCheckInfos\":[{\"CheckId\":65,\"CheckName\":\"New Check \",\"Frequency\":\"Weekly\"},{\"CheckId\":66,\"CheckName\":\"abc       \",\"Frequency\":\"Monthly\"},{\"CheckId\":67,\"CheckName\":\"2333      \",\"Frequency\":\"Weekly\"},{\"CheckId\":84,\"CheckName\":\"adafdasdfs\",\"Frequency\":\"Fortnightly\"},{\"CheckId\":85,\"CheckName\":\"Test check\",\"Frequency\":\"Fortnightly\"}],\"ProcessId\":9,\"ProcessName\":\"Process 2 Wasqur\"},{\"DataEntryCheckInfos\":[{\"CheckId\":65,\"CheckName\":\"New Check \",\"Frequency\":\"Weekly\"},{\"CheckId\":66,\"CheckName\":\"abc       \",\"Frequency\":\"Monthly\"},{\"CheckId\":67,\"CheckName\":\"2333      \",\"Frequency\":\"Weekly\"},{\"CheckId\":84,\"CheckName\":\"adafdasdfs\",\"Frequency\":\"Fortnightly\"},{\"CheckId\":85,\"CheckName\":\"Test check\",\"Frequency\":\"Fortnightly\"}],\"ProcessId\":14,\"ProcessName\":\"My New Procees\"}],\"DepartmentId\":8,\"DepartmentName\":\"Dept 7\"},{\"DataEntryProcessInfos\":[{\"DataEntryCheckInfos\":[{\"CheckId\":68,\"CheckName\":\"CHeck 1   \",\"Frequency\":\"Weekly\"},{\"CheckId\":73,\"CheckName\":\"New Check \",\"Frequency\":\"Fortnightly\"},{\"CheckId\":74,\"CheckName\":\"Checker   \",\"Frequency\":\"Weekly\"},{\"CheckId\":77,\"CheckName\":\"Wasi      \",\"Frequency\":\"Weekly\"}],\"ProcessId\":10,\"ProcessName\":\"My New Process\"},{\"DataEntryCheckInfos\":[{\"CheckId\":68,\"CheckName\":\"CHeck 1   \",\"Frequency\":\"Weekly\"},{\"CheckId\":73,\"CheckName\":\"New Check \",\"Frequency\":\"Fortnightly\"},{\"CheckId\":74,\"CheckName\":\"Checker   \",\"Frequency\":\"Weekly\"},{\"CheckId\":77,\"CheckName\":\"Wasi      \",\"Frequency\":\"Weekly\"}],\"ProcessId\":12,\"ProcessName\":\"Tes\"}],\"DepartmentId\":13,\"DepartmentName\":\"Dept 11\"},{\"DataEntryProcessInfos\":[{\"DataEntryCheckInfos\":[],\"ProcessId\":11,\"ProcessName\":\"222\"}],\"DepartmentId\":15,\"DepartmentName\":\"Dept 13\"},{\"DataEntryProcessInfos\":[{\"DataEntryCheckInfos\":[],\"ProcessId\":10,\"ProcessName\":\"My New Process\"},{\"DataEntryCheckInfos\":[],\"ProcessId\":12,\"ProcessName\":\"Tes\"}],\"DepartmentId\":13,\"DepartmentName\":\"Dept 11\"},{\"DataEntryProcessInfos\":[{\"DataEntryCheckInfos\":[],\"ProcessId\":13,\"ProcessName\":\"33\"}],\"DepartmentId\":14,\"DepartmentName\":\"Dept 12\"},{\"DataEntryProcessInfos\":[{\"DataEntryCheckInfos\":[],\"ProcessId\":9,\"ProcessName\":\"Process 2 Wasqur\"},{\"DataEntryCheckInfos\":[],\"ProcessId\":14,\"ProcessName\":\"My New Procees\"}],\"DepartmentId\":8,\"DepartmentName\":\"Dept 7\"}]"}

更多的事情,如果你可以帮助我,我想使用Jquery模板绑定这些数据,但我不想使用嵌套表,因为它会为流体模板创建问题以及标题将无法正确显示。这是我在上述问题中的模板。

 <tr>
    <td>
        <div class="btn-group  row-fluid" style="margin-top: 4px">
            <button class="btn">
                ${DepartmentId}</button>
            <button class="btn btn-success">
                    ${DepartmentName}</button></div>
    </td>
    <td colspan="4">
        <table class="table table-striped table-bordered dataTable" border="0" cellspancing="0"
            cellpadding="0" style="width: 100%">
            {{each DataEntryProcessInfos}}<tr>
                <td style="width: 20%">
                    <div class="btn-group row-fluid" style="margin-top: 4px">
                        <button class="btn">
                            ${ProcessId}</button>
                        <button class="btn btn-warning">
                                ${ProcessName}</button></div>
                </td>
                <td style="width: 60%">
                    <table style="width: 100%" class="table table-striped table-bordered dataTable">
                        {{each DataEntryCheckInfos}}<tr>
                            <td style="width: 33%">
                                <div class="btn-group row-fluid" style="margin-top: 4px">
                                    <button class="btn">
                                        ${CheckId}</button>
                                    <button class="btn btn-danger">
                                            ${CheckName}</button></div>
                            </td>
                            <td style="width: 33%">
                                <button class="btn">
                                    ${Frequency}
                                </button>
                            </td>
                            <td style="width: 33%">
                                <a class="btn-link" href="CheckItems.aspx?checkId=${CheckId}&mode=entry">Action
                                </a>
                            </td>
                        </tr>
                        {{/each}}
                    </table>
                </td>
            </tr>
            {{/each}}
        </table>
    </td>
</tr>

1 个答案:

答案 0 :(得分:0)

嵌套对象永远不会为空。他们是IEnumerable。但我认为你想要嵌套集合不为空的项目:

retvalue = retvalue.Where(x => x.DataEntryProcessInfoView.Any(
                               y => y.DataEntryCheckInfos.Any());

其中retvalueAsEnumerable().Select(x => new DataEntryInfoView()...之前的部分。