Home > Back-end >  how to use in clause in Linq Query and pass it dynamically from code
how to use in clause in Linq Query and pass it dynamically from code

Time:10-30

I am converting my project to EF Core in my old project I have a query running.

IDictionary<int, IGrouping<int, UserPurchaseItemAddonWithAmount>> addons = 
    context.Fetch<UserPurchaseItemAddonWithAmount>($"Select UPIA.*, EA.Amount From UserPurchaseItemAddons UPIA Inner Join ExtraAddons EA on UPIA.AddonID = EA.AddonID Where UPIA.UserPurchaseItemID in ({string.Join(',', userPurchaseItems.Select(S => S.UserPurchaseItemID))})")
        .GroupBy(G => G.UserPurchaseItemID).ToDictionary(D => D.Key);

I need to convert this query in to Linq query what I am doing is below

IDictionary<int, IGrouping<int, UserPurchaseItemAddonWithAmount>> addons = 
    (from f in context.UserPurchaseItemAddons
    join s in context.ExtraAddons
    on f.AddonId equals s.AddonId
    select new
    {
        Amount = s.Amount,
        UserPurchaseItemAddonID = f.UserPurchaseItemAddonId,
        UserPurchaseItemID = f.UserPurchaseItemId,
        BranchItemVariantID = f.BranchItemVariantId,
        AddonID = f.AddonId,
        UserID = f.UserId,
        IsDeleted = f.IsDeleted,
        ModifiedOn = f.ModifiedOn,
        ModifiedBy = f.ModifiedBy,
        Reason = f.Reason,
    }).GroupBy(G => G.UserPurchaseItemID).ToDictionary(D => D.Key);

This query is causing a compiler error related to casting to IGrouping<int, UserPurchaseItemAddonWithAmount> to an anonymous type. The other thing is that how can I apply in clause in where condition in above query, just like the first query .

class

 public class UserPurchaseItemAddonWithAmount
{

    public decimal Amount { get; set; }
     public int UserPurchaseItemAddonID { get; set; }
     public int UserPurchaseItemID { get; set; }
     public int BranchItemVariantID { get; set; }
     public int AddonID { get; set; }
     public int UserID { get; set; }
     public bool IsDeleted { get; set; }
     public DateTime? ModifiedOn { get; set; }
     public int? ModifiedBy { get; set; }
     public string? Reason { get; set; }
}

CodePudding user response:

Try the following query. Main mistake that you have returned anonymous class.

var purchaseItemIds = userPurchaseItems.Select(S => S.UserPurchaseItemID);

IDictionary<int, IGrouping<int, UserPurchaseItemAddonWithAmount>> addons = 
    (from f in context.UserPurchaseItemAddons
    join s in context.ExtraAddons on f.AddonId equals s.AddonId
    where purchaseItemIds.Contains(f.UserPurchaseItemID)
    select new UserPurchaseItemAddonWithAmount
    {
        Amount = s.Amount,
        UserPurchaseItemAddonID = f.UserPurchaseItemAddonId,
        UserPurchaseItemID = f.UserPurchaseItemId,
        BranchItemVariantID = f.BranchItemVariantId,
        AddonID = f.AddonId,
        UserID = f.UserId,
        IsDeleted = f.IsDeleted,
        ModifiedOn = f.ModifiedOn,
        ModifiedBy = f.ModifiedBy,
        Reason = f.Reason,
    })
    .AsEnumerable()
    .GroupBy(G => G.UserPurchaseItemID)
    .ToDictionary(D => D.Key);
  • Related