0

我正在使用实体框架 LINQ 来显示部门、处理和检查信息。每个部门有多个流程,每个流程可以有多个检查。我想以部门将只为多个进程显示一次的方式显示数据,并且每个进程的检查都应该显示在进程的前面。

这是示例。


| 部门 | 工艺 | 支票 |

| 1 部 | 过程A | 检查A

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

| 2 部 | 工艺 B | 检查A

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

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

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

| 3 部 | 过程A | 检查A

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

| 4 部 | 过程A | 我的案例中不应显示 Dept 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; 
    }

这是我不想显示那些没有 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>
4

1 回答 1

0

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

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

retvalue之前的部分在哪里AsEnumerable().Select(x => new DataEntryInfoView()...

于 2013-06-22T12:57:26.693 回答