【发布时间】:2021-06-30 18:21:18
【问题描述】:
假设我有两张桌子。我在这里只展示了必要的内容,但是ShoppingListA 和ShoppingListB 这两个表具有其他属性和对不同对象的 FK,但它们都具有“name”属性。
public class ShoppingListA
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ShoppingListB
{
public int Id { get; set; }
public string Name { get; set; }
}
我有以下 DTO
public class ShoppingListDto
{
public int Id { get; set; }
public string Name { get; set; }
public int Count {get; set;}
}
此外,我有一个回购方法:
public async Task<List<ShoppingListDto>> GetShoppingLists()
{
var shoppingListsA = await dbContext.ShoppingListA.Select(shoppingList => new ShoppingListDto
{
Id = shoppingList.Id,
Name = shoppingList.Name,
}).ToListAsync();
var shoppingListsB = await dbContext.ShoppingListB.Select(shoppingList => new ShoppingListDto
{
Id = shoppingList.Id,
Name = shoppingList.Name,
}).ToListAsync()
var allShoppingLists = shoppingListsA.Concat(shoppingListsB).ToList();
return allShoppingLists;
}
当我返回 shoppingListsDto 时,我想知道同名在两个表中出现的次数。举个例子。
假设 shoppingListA 是:
[
{
id: 2342352235,
name: "Weekend Groceries"
},
{
id: 457457543,
name: "Dinner party"
},
]
假设 shoppingListB 是:
[
{
id: 3795794697,
name: "BBQ stuff"
},
{
id: q845846q868,
name: "Dinner party"
},
]
然后我的 GetShoppingLists 方法将返回:
[
{
id: 3795794697,
name: "BBQ stuff",
count: 1,
},
{
id: q845846q868,
name: "Dinner party",
count: 2,
},
{
id: 2342352235,
name: "Weekend Groceries",
count: 1,
},
{
id: 457457543,
name: "Dinner party",
count: 2,
},
]
【问题讨论】:
-
这很简单,但没有 ID。
标签: linq linq-to-sql entity-framework-core