Web api 代码中的 LINQ 递归查询
LINQ Recursive query in web api code
需要在我的 Web Api
代码中通过 LINQ
编写递归查询。
table结构如下:
create table dbo.Test(
ID int primary key not null,
Name nvarchar(20) not null,
[Description] nvarchar(20) not null,
Parent_ID int)
ID Name Description Parent_ID
1001 BusinessUnit BU NULL
1002 BrandUnit Brand 1001
1003 Branch1 LondonBranch1 1002
1004 Branch2 LondonBranch2 1002
1005 Branch3 LondonBranch3 1002
我的网站API代码如下:
public class TestController : ApiController
{
public class TestDTO
{
public string Name { get; set; }
public string Desc { get; set; }
}
public TestDTO Get()
{
using (var db = new SampleEntities())
{
var hardcodedBusinessUnitID=1001;
var xx = db.Tests.FirstOrDefault(x => x.ID.Equals(hardcodedBusinessUnitID)).ID;
List<int> list=new List<int>();
list.Add(xx);
var idFromUI=1009;
return new TestDTO
{
Name = list.Contains(idFromUI) ? "Match Found" : string.Empty,
Desc = "Blah Blah"
};
}
}
}
我不知何故需要获得 ID
1001 的所有 children(因为 1001 有一个 ID 为 1002 的 child,它还有 3 个 children ID 为 1003,1004 和 1005) 通过 LINQ
查询从数据库中获取并将它们绑定到 list
.
Post 我将从 UI
得到一个 ID
并检查 UI
的 ID
是否存在于该列表中。
我做不到,因为我对LINQ
不是很了解。
请专家帮忙
您需要一个用于查找的临时项目列表children;您清空列表并将 children 放入该列表并重复。大概是这样的:
var parent = db.Tests.FirstOrDefault(x => x.ID.Equals(hardcodedBusinessUnitID)).ID;
// a list of the type of your object, to store all the items
var items = new List<Test>();
items.Add(parent)
// make a new list that will be used to search the table
searchItems = new List<Test>(items);
while (searchItems.Any())
{
var ids = searchItems.Select(i => i.ID).ToList();
searchItems = db.Tests.Where(t =>
t.Parent_ID.HasValue && ids.Contains(t.Parent_ID.Value)).ToList();
items.AddRange(searchItems);
}
List<int> list = items.Select(i => i.ID).ToList();
你可能会有不同的看法。使用您从 UI 中查找的 ID 开始搜索,然后在层次结构中向上搜索,直到找到没有 parent 的测试记录。然后将没有 parent ID 的测试记录与您的硬编码 ID 进行比较。如果它们匹配,那么来自 UI 的 id 是一个 child;否则不行。
var t = db.Tests.SingleOrDefault(x => x.ID == idFromUI);
if(t != null){
int? parentId = t.Parent_ID;
int rootParentId = -1;
while(parentId != null){
var currentTest = db.Tests.SingleOrDefault(x => x.ID == parentId);
if(currentTest != null){
parentId = currentTest.Parent_ID;
rootParentId = currentTest.ID;
}
else{
parentId = null;
}
}
if(rootParentId == hardcodedBusinessUnitID){
return new TestDTO
{
Name = "Match Found",
Desc = "Blah Blah"
};
}
}
return new TestDTO
{
Name = string.Empty,
Desc = "Blah Blah"
};
需要在我的 Web Api
代码中通过 LINQ
编写递归查询。
table结构如下:
create table dbo.Test(
ID int primary key not null,
Name nvarchar(20) not null,
[Description] nvarchar(20) not null,
Parent_ID int)
ID Name Description Parent_ID
1001 BusinessUnit BU NULL
1002 BrandUnit Brand 1001
1003 Branch1 LondonBranch1 1002
1004 Branch2 LondonBranch2 1002
1005 Branch3 LondonBranch3 1002
我的网站API代码如下:
public class TestController : ApiController
{
public class TestDTO
{
public string Name { get; set; }
public string Desc { get; set; }
}
public TestDTO Get()
{
using (var db = new SampleEntities())
{
var hardcodedBusinessUnitID=1001;
var xx = db.Tests.FirstOrDefault(x => x.ID.Equals(hardcodedBusinessUnitID)).ID;
List<int> list=new List<int>();
list.Add(xx);
var idFromUI=1009;
return new TestDTO
{
Name = list.Contains(idFromUI) ? "Match Found" : string.Empty,
Desc = "Blah Blah"
};
}
}
}
我不知何故需要获得 ID
1001 的所有 children(因为 1001 有一个 ID 为 1002 的 child,它还有 3 个 children ID 为 1003,1004 和 1005) 通过 LINQ
查询从数据库中获取并将它们绑定到 list
.
Post 我将从 UI
得到一个 ID
并检查 UI
的 ID
是否存在于该列表中。
我做不到,因为我对LINQ
不是很了解。
请专家帮忙
您需要一个用于查找的临时项目列表children;您清空列表并将 children 放入该列表并重复。大概是这样的:
var parent = db.Tests.FirstOrDefault(x => x.ID.Equals(hardcodedBusinessUnitID)).ID;
// a list of the type of your object, to store all the items
var items = new List<Test>();
items.Add(parent)
// make a new list that will be used to search the table
searchItems = new List<Test>(items);
while (searchItems.Any())
{
var ids = searchItems.Select(i => i.ID).ToList();
searchItems = db.Tests.Where(t =>
t.Parent_ID.HasValue && ids.Contains(t.Parent_ID.Value)).ToList();
items.AddRange(searchItems);
}
List<int> list = items.Select(i => i.ID).ToList();
你可能会有不同的看法。使用您从 UI 中查找的 ID 开始搜索,然后在层次结构中向上搜索,直到找到没有 parent 的测试记录。然后将没有 parent ID 的测试记录与您的硬编码 ID 进行比较。如果它们匹配,那么来自 UI 的 id 是一个 child;否则不行。
var t = db.Tests.SingleOrDefault(x => x.ID == idFromUI);
if(t != null){
int? parentId = t.Parent_ID;
int rootParentId = -1;
while(parentId != null){
var currentTest = db.Tests.SingleOrDefault(x => x.ID == parentId);
if(currentTest != null){
parentId = currentTest.Parent_ID;
rootParentId = currentTest.ID;
}
else{
parentId = null;
}
}
if(rootParentId == hardcodedBusinessUnitID){
return new TestDTO
{
Name = "Match Found",
Desc = "Blah Blah"
};
}
}
return new TestDTO
{
Name = string.Empty,
Desc = "Blah Blah"
};