简体   繁体   中英

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

I have two tables by name formcontrols and otherfields both table has one common column by name FormId The basic understanding is a from can have multiple formcontrols and multiple otherfields with same formID

Below is the screenshort for formcontrols table

在此处输入图像描述

Below is the screenshort for otherfields table

在此处输入图像描述

As we can see from above two tables we have common FormID as 10

i need to write a linq query to get all this data in one request i tried doing it but i am getting duplicate otherfield object on each loop below is my code

 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();

The Json Response which i am getting is

[
  {
    "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
    }
  }
]

As we can see from the above response i am getting the duplication of other field

Below is my model class

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

this is the combination of two model class name formcontrols and otherfields

Below are the model classes for formcontrols table and otherfields

 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; }

    }

Change FormControlsDto to:

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

And then use the following query:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM