一直在试图找出我的查询有什么问题,并且一直在玩Where的条件,但没有运气。下面是我在控制器文件中的查询。
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
答案 0 :(得分:1)
让我们尝试简化这里的事情。
利用Entity Framework延迟加载方法和建模,我假设你的实体建模是正确的,你可以做一些像(只是一个例子)。
//Area Of Interest
var areaOfInterest = (from AreaOfInterest in db.AreaOfInterests.Include(a => a.AOIValueDrivers)
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 = a.AOIValueDrivers.Where(vd => vd.AOIId == a.AOIId).ToList()
});
}
您不需要执行两个查询,因为当您对具有其他实体相关的实体执行选择时,实体框架会延迟加载该关系。
在此处https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx和http://www.entityframeworktutorial.net/EntityFramework4.3/lazy-loading-with-dbcontext.aspx
了解详情答案 1 :(得分:0)
我对此并不了解,但根据代码,您的linq查询不正确。查询中的where子句是错误的。
如果您想要AOIID == 1的项目,则以下代码将起作用。
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 == 1
select aoivd
).ToList();