0

I'm trying to use LINQ to pull an ItemGroup from the database but initialize its SalesId as well from the "sales" table, but there are cases where the sales entity doesn't exist for that ItemGroup, and I still want to return that ItemGroup with a default SalesID = 0. I've tried using the following LINQ query, which isn't working:

public async Task<ItemGroup> GetAsync(int itemGroupId)
{
    var query =
        from itemGroup in contextSales.items
        join salesGroup in contextSales.sales
            on itemGroup.SalesId equals salesGroup.SalesId
            into itemsSoldGroup
        from sales in itemsSoldGroup
        orderby itemGroup.ItemGroupId
        where itemGroup.ItemGroupId == itemGroupId
        select new ItemGroup()
        {
            ItemGroupId = itemGroup.ItemGroupId,
            Description = itemGroup.Description,
            SalesId = sales != null ? sales.SalesId : 0
        };

    return await query.FirstOrDefaultAsync().ConfigureAwait(false);
}
AtomicallyBeyond
  • 348
  • 1
  • 15

0 Answers0