[英]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我有两个名为 formcontrols 和其他字段的表,两个表都有一个名为 FormId 的公共列基本理解是一个 from 可以有多个 formcontrols 和多个具有相同 formID 的其他字段
Below is the screenshort for formcontrols table下面是 formcontrols 表的屏幕截图
Below is the screenshort for otherfields table以下是其他字段表的屏幕截图
As we can see from above two tables we have common FormID as 10正如我们从上面两个表中看到的,我们有共同的 FormID 为 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我需要编写一个 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();
The Json Response which i am getting is我得到的 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
}
}
]
As we can see from the above response i am getting the duplication of other field正如我们从上面的回复中看到的,我得到了其他字段的重复
Below is my model class下面是我的 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这是两个 model class name formcontrols 和 otherfields 的组合
Below are the model classes for formcontrols table and 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; }
}
Change FormControlsDto
to:将
FormControlsDto
更改为:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.