一直在試圖弄清楚我的查詢出了什麼問題,並且一直在玩Where條件的一輪,但沒有運氣。以下是我在控制器文件中的查詢。Linq在條件無法正常工作的情況下,使用EF
ValueStory valuestory = await db.ValueStories.FindAsync(id);
//Area Of Interest Value Drivers
var aoivaluedriver = (from AOIValueDrivers in db.AOIValueDrivers
join aoivd in db.AOIValueDrivers on AOIValueDrivers.AOIVDId equals aoivd.AOIId
join aoi in db.AreaOfInterests on aoivd.AOIId equals aoi.AOIId
join vs in db.ValueStories on aoi.Id equals vs.Id
where aoivd.AOIId == aoi.AOIId && aoi.Id == vs.Id && vs.Id == id
select aoivd
).ToList();
List<AOIValueDrivers> aoivd1 = new List<AOIValueDrivers>();
foreach (var a in aoivaluedriver)
{
aoivd1.Add(new AOIValueDrivers()
{
AOIVDId = a.AOIVDId,
Item = a.Item,
SubItem = a.SubItem,
Value = a.Value,
AOIId = a.AOIId
});
}
//Area pf Interest Value Drivers End
//Area Of Interest
var areaOfInterest = (from AreaOfInterest in db.AreaOfInterests
join aoi in db.AreaOfInterests on AreaOfInterest.AOIId equals aoi.AOIId
join vs in db.ValueStories on aoi.Id equals vs.Id
where aoi.Id == vs.Id && vs.Id == id
select aoi
//{
// AOIName = aoe.AOIName,
//ValueDriver = aoe.ValueDriver
//}
).ToList();
List<AreaOfInterest> aoi1 = new List<AreaOfInterest>();
foreach (var a in areaOfInterest)
{
aoi1.Add(new AreaOfInterest()
{
AOIId = a.AOIId,
AOIName = a.AOIName,
Selected = a.Selected,
Id = a.Id,
AOIValueDrivers = aoivd1
});
}
在我的Json結果是它列出我所有的aoivaluedriver數據昂忽略我的Where條件。這裏是JSON輸出
{
"AreaOfInterest": [
{
"AOIId": 1,
"AOIName": "Supply Chain/ Direct Materials",
"Selected": true,
"Id": 1,
"ValueStory": null,
"AOIValueDrivers": [
{
"AOIVDId": 1,
"Item": "Negotiate better prices & conditions",
"SubItem": "Automate the process of sourcing of direct materials and integrate it to you ERP and key execution systems",
"Value": 3,
"AOIId": 1,
"AreaOfInterest": null
},
{
"AOIVDId": 2,
"Item": "Lower Cost",
"SubItem": "Reduct costs of supply chain FTEs",
"Value": 3,
"AOIId": 1
},
{
"AOIVDId": 10,
"Item": "Negotiate better prices & conditions",
"SubItem": "Foster supplier competition to reduce pricing and obtain best market value",
"Value": 3,
"AOIId": 2
},
{
"AOIVDId": 19,
"Item": "Negotiate better prices & conditions",
"SubItem": "Control and maximise savings on non strategically source spend",
"Value": 3,
"AOIId": 3
},
{
"AOIVDId": 24,
"Item": "Buy and pay at the negotiated rate & conditions",
"SubItem": "Reduce invoice matching exceptions (to contracts, orders, & receipts)",
"Value": 3,
"AOIId": 4
},
{
"AOIVDId": 34,
"Item": "Free up working capital",
"SubItem": "Offer suppliers options to get paid quicker while not reducing DPO",
"Value": 3,
"AOIId": 5
},
{
"AOIVDId": 38,
"Item": "Protect your revenue",
"SubItem": "Access to systems such as Dun & Bradstreet to certify suppliers",
"Value": 3,
"AOIId": 6
},
{
"AOIVDId": 43,
"Item": "nothing",
"SubItem": "nothing",
"Value": 0,
"AOIId": 7
}
]
}
]
}
預期的結果是,它應該只AOIId = 1,而是其返回的所有表中的項目將返回所有項目。我的查詢有什麼問題?對EF和Linq仍然很陌生,仍然試圖讓自己熟悉它。
這裏是我的areaofinterest
CREATE TABLE [dbo].[AreaOfInterest] (
[AOIId] INT IDENTITY (1, 1) NOT NULL,
[AOIName] NVARCHAR (MAX) NOT NULL,
[Selected] BIT NOT NULL,
[Id] INT NOT NULL,
CONSTRAINT [PK_dbo.AreaOfInterest] PRIMARY KEY CLUSTERED ([AOIId] ASC),
CONSTRAINT [FK_dbo.AreaOfInterest_dbo.ValueStory_Id] FOREIGN KEY ([Id]) REFERENCES [dbo].[ValueStory] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_Id]
ON [dbo].[AreaOfInterest]([Id] ASC);
數據庫建模,這裏是一個爲areaofinterestvaluedriver
CREATE TABLE [dbo].[AOIValueDrivers] (
[AOIVDId] INT IDENTITY (1, 1) NOT NULL,
[Item] NVARCHAR (MAX) NULL,
[SubItem] NVARCHAR (MAX) NULL,
[Value] INT NOT NULL,
[AOIId] INT NOT NULL,
CONSTRAINT [PK_dbo.AOIValueDrivers] PRIMARY KEY CLUSTERED ([AOIVDId] ASC),
CONSTRAINT [FK_dbo.AOIValueDrivers_dbo.AreaOfInterest_AOIId] FOREIGN KEY ([AOIId]) REFERENCES [dbo].[AreaOfInterest] ([AOIId]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_AOIId]
ON [dbo].[AOIValueDrivers]([AOIId] ASC);
預期的JSON輸出應該是這樣的
{
"AreaOfInterest": [
{
"AOIId": 1,
"AOIName": "Supply Chain/ Direct Materials",
"Selected": true,
"Id": 1,
"ValueStory": null,
"AOIValueDrivers": [
{
"AOIVDId": 1,
"Item": "Negotiate better prices & conditions",
"SubItem": "Automate the process of sourcing of direct materials and integrate it to you ERP and key execution systems",
"Value": 3,
"AOIId": 1,
"AreaOfInterest": null
}
]
}
],
"AreaOfInterest": [
{
"AOIId": 2,
"AOIName": "Supply Chain/ Direct Materials",
"Selected": true,
"Id": 1,
"ValueStory": null,
"AOIValueDrivers": [
{
"AOIVDId": 10,
"Item": "Negotiate better prices & conditions",
"SubItem": "Foster supplier competition to reduce pricing and obtain best market value",
"Value": 3,
"AOIId": 2
}
]
}
],
"AreaOfInterest": [
{
"AOIId": 3,
"AOIName": "Supply Chain/ Direct Materials",
"Selected": true,
"Id": 1,
"ValueStory": null,
"AOIValueDrivers": [
{
"AOIVDId": 19,
"Item": "Negotiate better prices & conditions",
"SubItem": "Control and maximise savings on non strategically source spend",
"Value": 3,
"AOIId": 3
}
]
}
]
}
這是怎麼了我的模特看起來:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace WebService.Models
{
public class AreaOfInterest
{
[Key]
public int AOIId { get; set; }
[Required]
public string AOIName { get; set; }
public bool Selected { get; set; }
// Foreign Key
public int Id { get; set; }
// Navigation property
public virtual ValueStory ValueStory { get; set; }
//Value Drivers AOI for the Value Story
public List<AOIValueDrivers> AOIValueDrivers { get; set; }
}
public class AOIValueDrivers
{
[Key]
public int AOIVDId { get; set; }
public string Item { get; set; }
public string SubItem { get; set; }
public int Value { get; set; }
// Foreign Key
public int AOIId { get; set; }
// Navigation property
public virtual AreaOfInterest AreaOfInterest { get; set; }
}
}
這是工作的代碼
//Area Of Interest Value Drivers
var aoivaluedriver = (from AOIValueDrivers in db.AOIValueDrivers
join aoivd in db.AOIValueDrivers on AOIValueDrivers.AOIVDId equals aoivd.AOIId
join aoi in db.AreaOfInterests on aoivd.AOIId equals aoi.AOIId
select aoivd
).ToList();
List<AOIValueDrivers> aoivd1 = new List<AOIValueDrivers>();
foreach (var a in aoivaluedriver)
{
aoivd1.Add(new AOIValueDrivers()
{
AOIVDId = a.AOIVDId,
Item = a.Item,
SubItem = a.SubItem,
Value = a.Value,
AOIId = a.AOIId
});
}
//Area pf Interest Value Drivers End
//Area Of Interest
var areaOfInterest = (from AreaOfInterest in db.AreaOfInterests
join aoi in db.AreaOfInterests on AreaOfInterest.AOIId equals aoi.AOIId
join vs in db.ValueStories on aoi.Id equals vs.Id
where aoi.Id == vs.Id && vs.Id == id
select aoi).ToList();
List<AreaOfInterest> aoi1 = new List<AreaOfInterest>();
foreach (var a in areaOfInterest)
{
aoi1.Add(new AreaOfInterest()
{
AOIId = a.AOIId,
AOIName = a.AOIName,
Selected = a.Selected,
Id = a.Id,
AOIValueDrivers = aoivd1?.Where(vd => vd.AOIId == a.AOIId)?.ToList()
});
}
//Area pf Interest End
您的查詢是沒有意義的。爲什麼你自己加入'db.AreaOfInterests',然後將它加入到'db.ValueStories'中,但從不使用值'db.ValueStories'? – DavidG
你可以發佈你的數據庫建模這些表嗎?我認爲你有一個懶加載問題,因爲你的表關係。 –
@Erick,我已經更新了我的帖子,數據庫結構 –