第一次使用LinqtoSQL。所以,给你们一些背景:LinqtoSQL从其他表获取值
我有一个名为库存具有以下字段一个简单的SQL Server表
- InventoryID(INT)(自动增量)
- InventoryItemName(VARCHAR)(255)
- InventoryCategory(INT)
接下来我有具有以下字段的另一个表名为InventoryCategories:
- InventoryCategoryID(INT)(自动递增)
- InventoryCategoryName(VARCHAR)(255)
- InventoryCategoryDescription(VARCHAR)(255)
接着,目前我有其选择哪个查询的组合框要更新DataGrid.ItemSource,其代码fo如下所示
if (searchcategory == "All Stock")
{
InventoryDataContext dc = new InventoryDataContext();
var q =
from a in dc.GetTable<Inventory>()
select a;
SearchResults.ItemsSource = q;
}
现在,此结果将返回带有列的Inventory整个表InventoryID,InventoryItemName和InventoryCategory。但是,它返回InventoryCategory列中的InventoryCategory的ID号。
任何人都可以帮助我在Inventory查询中从InventoryCategories表中获取InventoryCategoryName而不是ID吗?这将需要什么?
您将需要加入的'InventoryCategoryID'两个表,并选择一个新的键入(可能是匿名),在其中选择名称而不是完整的“库存”对象。建议您制作一个适合您的视图。然后,您可以简单地查询 –
请检查文章,https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/join-clause。左外部连接应该适合你的任务 – ASpirin
你想为所选InventoryID的InventoryCategoryName? – QweRty