Mihir Shah
Mihir Shah

Reputation: 988

Wanted to perform a Join but keep the IQueryable behavior as is

I have a table named ProductIsNotifyMe for Notify Me functionality on ECommerce application, in which columns would be

StoreID
MemberID
Sku (Product)
LocationId
Unit.

Also I have other products master tables like Product, ProductPricing, ProductCustomerPricing and more. The challenges are, I have column AvailableIn in Product table in which units are available as JSON like

[
    {
        "Unit": "KG",
        "Factor": 1
    },
    {
        "Unit": "BAG",
        "Factor": 6
    },
    {
        "Unit": "CTN",
        "Factor": 20
    }
]

Now I have IQueryable query which just constructed the query and won't be make the database call. And I wanted to keep this behavior same and same time wanted add Join ProductIsNotifyMe table into it but due to Units are stored in JSON in product table, how would I make a join.

Below is my IQueryable LINQ query

return from prod in products

    join price in db.ProductPrices
        on new { prod.StoreId, prod.Sku, PriceSetCode = ctx.PriceSetCode.Value }
        equals new { price.StoreId, price.Sku, price.PriceSetCode } into prices
    from price in prices.DefaultIfEmpty()

    join custPrice in db.ProductCustomerPrices
        on new
        {
            prod.StoreId,
            prod.Sku,
            PriceSetCode = ctx.PriceSetCode.Value,
            AccountId = ctx.AccountId.Value.Value
        }
        equals new
        {
            custPrice.StoreId,
            custPrice.Sku,
            custPrice.PriceSetCode,
            custPrice.AccountId
        } into custPrices
    from custPrice in custPrices.DefaultIfEmpty()

    join custGroupPrice in db.ProductCustGroupPrices
        on new
        {
            prod.StoreId,
            prod.Sku,
            DiscountGroupCode = ctx.DiscountGroupCode,
            PriceSetCode = ctx.PriceSetCode.Value
        }
        equals new
        {
            custGroupPrice.StoreId,
            custGroupPrice.Sku,
            custGroupPrice.DiscountGroupCode,
            custGroupPrice.PriceSetCode
        } into custGroupPrices
    from custGroupPrice in custGroupPrices.DefaultIfEmpty()

    join locationStock in db.ProductLocations
        on new { prod.StoreId, prod.Sku, Id = ctx.LocationId }
        equals new { locationStock.StoreId, locationStock.Sku, locationStock.Id } into locationStocks
    from locationStock in locationStocks.DefaultIfEmpty()

    join lastOrdQty in db.LastOrderQty
        on new { prod.Sku, AccountId = ctx.AccountId.Value.Value }
        equals new { lastOrdQty.Sku, lastOrdQty.AccountId } into lastOrdQtys
    from lastOrdQty in lastOrdQtys.DefaultIfEmpty()

    join memberInfo in db.ProductMemberInfo
        on new { prod.StoreId, prod.Sku, MemberId = memId }
        equals new { memberInfo.StoreId, memberInfo.Sku, memberInfo.MemberId } into memberInfos
    from memberInfo in memberInfos.DefaultIfEmpty()

    join category in db.Categories
        on new { p = prod.PrimaryCategoryId ?? Guid.Empty }
        equals new { p = category.Id } into categoryInfos
    from categoryInfo in categoryInfos.DefaultIfEmpty()

    join substitute in db.ProductSubstitutes
        on new
        {
            prod.Sku
        }
        equals new
        {
            substitute.Sku
        } into prodSubstitute
    from substitute in prodSubstitute.DefaultIfEmpty()
    
    orderby prod.Sku

    select new ProductQuery
    {
        product = prod,
        price = price,
        custPrice = custPrice,
        categoryInfo = categoryInfo,
        custGroupPrice = custGroupPrice,
        onHand = locationStock.OnHand,
        onHandInf = locationStock.OnHand == null && locationStock.Id != null,
        limit = locationStock.SaleLimit,
        limitInf = locationStock.SaleLimit == null && locationStock.Id != null,
        locenabled = locationStock.Allowed && locationStock.Id != null,
        locsku = locationStock.AlternateSku,
        memberInfo = memberInfo,
        lastOrdQty = lastOrdQty.LastOrderQty, // Change For Last Order Quantity
        lastOrdDate = lastOrdQty.LastOrderTimestamp == null
            ? DateTimeOffset.Now
            : lastOrdQty.LastOrderTimestamp,
        substituteAvail = (substitute != null && !string.IsNullOrEmpty(substitute.ItemSub1)),
        substitutes = substitute != null ? new string[]
            {
                (string.IsNullOrEmpty(substitute.ItemSub1) ? string.Empty : substitute.ItemSub1),
                (string.IsNullOrEmpty(substitute.ItemSub2) ? string.Empty : substitute.ItemSub2),
                (string.IsNullOrEmpty(substitute.ItemSub3) ? string.Empty : substitute.ItemSub3)
            } : new string[0],
    };

I wanted to add a join for ProductIsNotifyMe table on columns StoreID, MemberID, SKU, LocationID and Unit. If not possible for Unit then I wanted to make a Join for StoreId, MemberID, Sku, LocationID and in select clause, I want Sku and List of Units as List or IEnumerable but same time wanted to keep the IQueryable behavior too.

Upvotes: 0

Views: 58

Answers (0)

Related Questions