Reputation: 33
First time using LinqtoSQL. So to give you guys a bit of context:
I have a simple SQL Server table called Inventory with the following fields
Next I have another table called InventoryCategories for with the following fields:
Next, Currently I have a combo box which selects which query to update the DataGrid.ItemSource, The code fo for this is below
if (searchcategory == "All Stock")
{
InventoryDataContext dc = new InventoryDataContext();
var q =
from a in dc.GetTable<Inventory>()
select a;
SearchResults.ItemsSource = q;
}
Now this result returns the Full table of Inventory with the columns of InventoryID, InventoryItemName, and InventoryCategory. However it returns the ID Number of InventoryCategory, in the InventoryCategory Column.
Would anyone be able to help me get the InventoryCategoryName from InventoryCategories Table in this query instead of the ID? What would be required for this?
Upvotes: 1
Views: 60
Reputation: 1301
try using left join:
var qry = from inv in context.Inventory
from invCategory in context.InventoryCategories.Where(u => inv.InventoryCategory == u.InventoryCategoryID).DefaultIfEmpty()
select new myViewModel {id = invCategory.InventoryCategoryID, categoryName = invCategory .InventoryCategoryName }
and don't forget to create myViewModel class with id
and categoryName
properties
Upvotes: 1