繁体   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