簡體   English   中英

使用實體框架核心和 asp.net 核心 webapi 連接兩個表

[英]joining two tables using entity Framework core and asp.net core webapi

我有兩個名為 formcontrols 和其他字段的表,兩個表都有一個名為 FormId 的公共列基本理解是一個 from 可以有多個 formcontrols 和多個具有相同 formID 的其他字段

下面是 formcontrols 表的屏幕截圖

在此處輸入圖像描述

以下是其他字段表的屏幕截圖

在此處輸入圖像描述

正如我們從上面兩個表中看到的,我們有共同的 FormID 為 10

我需要編寫一個 linq 查詢以在一個請求中獲取所有這些數據我嘗試這樣做但是我在下面的每個循環中得到重復的其他字段 object 是我的代碼

 var formControls = await 
 (
    from d in _context.FormControls 
    join f in _context.OtherFields on d.FormId equals f.FormID 
    where d.FormId == request.FormId 
    select new FormControlsDto
    {
        FormControls = d,
        OtherFields = f
    }
 ).Distinct().ToListAsync();

我得到的 Json 響應是

[
  {
    "formControls": {
      "formId": 10,
      "controlName": "code",
      "fieldName": "code",
      "icon": null,
      "arbCaption": "code",
      "engCaption": "code",
      "maxLength": "",
      "minLength": "",
      "maxValue": "",
      "minValue": "",
      "dataType": "text",
      "isHidden": false,
      "defaultValue": "",
      "defaultValueScalarFun": "",
      "required": true,
      "regExpression": "",
      "controlType": "textbox",
      "searchId": 0,
      "anotherCriteria": "",
      "disabled": false,
      "indexPage": true,
      "width": 0,
      "section": 1,
      "autoGenerate": false,
      "rank": 0,
      "engToolTip": "FirstName",
      "arbToolTip": "FirstName",
      "id": 1,
      "createdDate": "2023-01-29T09:40:24.0446711",
      "updatedDate": "2023-01-29T09:40:24.0446711",
      "modifyUserId": 0,
      "regUserId": 0
    },
    "otherFields": {
      "formID": 10,
      "width": 0,
      "rank": 3,
      "isHidden": false,
      "engName": "Cost",
      "arbName": "Cost",
      "fieldType": "textbox",
      "dataType": "text",
      "dataLength": 0,
      "remarks": null,
      "isFilter": true,
      "isInList": true,
      "modifyDate": "2023-01-30T00:00:00",
      "id": 4,
      "createdDate": "2023-01-30T00:00:00",
      "updatedDate": "2023-01-30T00:00:00",
      "modifyUserId": 0,
      "regUserId": 0
    }
  },
  {
    "formControls": {
      "formId": 10,
      "controlName": "EngName",
      "fieldName": "EngName",
      "icon": null,
      "arbCaption": "EngName",
      "engCaption": "EngName",
      "maxLength": null,
      "minLength": null,
      "maxValue": null,
      "minValue": null,
      "dataType": "text",
      "isHidden": false,
      "defaultValue": null,
      "defaultValueScalarFun": null,
      "required": true,
      "regExpression": null,
      "controlType": "textbox",
      "searchId": 0,
      "anotherCriteria": null,
      "disabled": false,
      "indexPage": true,
      "width": 0,
      "section": 1,
      "autoGenerate": false,
      "rank": 1,
      "engToolTip": "EngName",
      "arbToolTip": "EngName",
      "id": 6,
      "createdDate": "2023-01-30T00:00:00",
      "updatedDate": "2023-01-30T00:00:00",
      "modifyUserId": 0,
      "regUserId": 0
    },
    "otherFields": {
      "formID": 10,
      "width": 0,
      "rank": 3,
      "isHidden": false,
      "engName": "Cost",
      "arbName": "Cost",
      "fieldType": "textbox",
      "dataType": "text",
      "dataLength": 0,
      "remarks": null,
      "isFilter": true,
      "isInList": true,
      "modifyDate": "2023-01-30T00:00:00",
      "id": 4,
      "createdDate": "2023-01-30T00:00:00",
      "updatedDate": "2023-01-30T00:00:00",
      "modifyUserId": 0,
      "regUserId": 0
    }
  },
  {
    "formControls": {
      "formId": 10,
      "controlName": "ArbName",
      "fieldName": "ArbName",
      "icon": null,
      "arbCaption": "ArbName",
      "engCaption": "ArbName",
      "maxLength": null,
      "minLength": null,
      "maxValue": null,
      "minValue": null,
      "dataType": "text",
      "isHidden": false,
      "defaultValue": null,
      "defaultValueScalarFun": null,
      "required": true,
      "regExpression": null,
      "controlType": "textbox",
      "searchId": 0,
      "anotherCriteria": null,
      "disabled": false,
      "indexPage": true,
      "width": 0,
      "section": 1,
      "autoGenerate": false,
      "rank": 2,
      "engToolTip": "ArbName",
      "arbToolTip": "ArbName",
      "id": 7,
      "createdDate": "2023-01-30T00:00:00",
      "updatedDate": "2023-01-30T00:00:00",
      "modifyUserId": 0,
      "regUserId": 0
    },
    "otherFields": {
      "formID": 10,
      "width": 0,
      "rank": 3,
      "isHidden": false,
      "engName": "Cost",
      "arbName": "Cost",
      "fieldType": "textbox",
      "dataType": "text",
      "dataLength": 0,
      "remarks": null,
      "isFilter": true,
      "isInList": true,
      "modifyDate": "2023-01-30T00:00:00",
      "id": 4,
      "createdDate": "2023-01-30T00:00:00",
      "updatedDate": "2023-01-30T00:00:00",
      "modifyUserId": 0,
      "regUserId": 0
    }
  }
]

正如我們從上面的回復中看到的,我得到了其他字段的重復

下面是我的 model class

 public class FormControlsDto
    {
        public FormControls FormControls { get; set; }
        public OtherFields OtherFields { get; set; }
    }

這是兩個 model class name formcontrols 和 otherfields 的組合

以下是表單控件表和其他字段的 model 類

 public class FormControls : BaseEntity
    {
        public int FormId { get; set; }
        public string ControlName { get; set; }
        public string FieldName { get; set; }
        public string? Icon { get; set; }
        public string ArbCaption { get; set; }
        public string? EngCaption { get; set; }
        public string? MaxLength { get; set; }
        public string? MinLength { get; set; }
        public string? MaxValue { get; set; }
        public string? MinValue { get; set; }
        public string? DataType { get; set; }
        public bool? IsHidden { get; set; }
        public string? DefaultValue { get; set; }
        public string? DefaultValueScalarFun { get; set; }
        public bool? Required { get; set; }
        public string? RegExpression { get; set; }
        public string ControlType { get; set; }
        public int? SearchId { get; set; }
        public string? AnotherCriteria { get; set; }
        public bool? Disabled { get; set; }
        public bool? IndexPage { get; set; }
        public int? Width { get; set; }
        public int? Section { get; set; }
        public bool? AutoGenerate { get; set; }
        public int? Rank { get; set; }
        public string? EngToolTip { get; set; }
        public string? ArbToolTip { get; set; }


    }
 public class OtherFields : BaseEntity
    {

        public int FormID { get; set; }

        public int? Width { get; set; }

        public int? Rank { get; set; }
        public bool IsHidden { get; set; }

        public string? EngName { get; set; }

        public string? ArbName { get; set; }

        public string? FieldType { get; set; }

        public string? DataType { get; set; }

        public short? DataLength { get; set; }

        public string? Remarks { get; set; }

        public bool  IsFilter { get; set; }
        public bool IsInList { get; set; }

        public DateTime? ModifyDate { get; set; }

    }

FormControlsDto更改為:

public class FormControlsDto
{
    public FormControls FormControls { get; set; }
    public List<OtherFields> OtherFields { get; set; }
}

然后使用以下查詢:

 var formControls = await _context.FormControls 
    .Select(d => new FormControlsDto
    {
        FormControls = d,
        OtherFields = _context.OtherFields.Where(f => d.FormId == f.FormId)
            .ToList()
    })
    .ToListAsync();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM