0

First off, I'm using Entity Framework Core 5 with SQL Server provider and a repository pattern to do any work related to the infrastructure.

My problem is that I'm trying to query for a list of CardSale entities while matching on a list of parameters.

The CardSale entity has no key currently, it uses a composite clustered index for uniqueness and organization. we went for this kind of index for several reasons including GroupBy query optimization.

So if I want to query for a single record I can just use something simple like this:

            return await _appDbContext.CardSales
                .Where(c => 
                c.PurchaseCurrency == currency &&
                c.Date == date &&
                c.SubcategoryId == subcategoryId &&
                c.CardId == cardId &&
                c.RegionId == regionId &&
                c.AccountId == accountId)
                .Include(c => c.Card)
                .Include(c => c.Region)
                .Include(c => c.Account)
                .FirstOrDefaultAsync();

Now the problem lies in the next step, a portion of the code requires the ability to pass in a list of Notification objects to a query and receive a list of matching CardSale object on the properties mentioned above.

Before the current change in the code base we had a really big string Key property which was just the properties mentioned above interpolated into a single string (I know, it was really bad) so that meant I could pass in a List<string> ids and use something like

return await _appDbContext.CardSales
    .Where(c => ids.Contains(c.id)

and it works just fine.

But now my problem is that this big string id doesn't exist anymore, I only have the individual properties and the composite clustered index.

How can I query for multiple CardSale entities while using parameters from multiple Notification objects?

Extra notes:

All ids are Guid. Date is a date component of a DateTime object.

The query for a specific CardSale has to contain ALL the mentioned properties in the first query which all also exist in the Notification object.

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
  • 1
    If there is not too many filter items, you can use extension method [FilterByItems](https://stackoverflow.com/a/67666993/10646316) and filter your records in the following way: `_appDbContext.CardSales.FilterByItems(notifications, (c, n) => c.PurchaseCurrency == n.PurchaseCurrency && c.Date == n.Date && ..., true)` – Svyatoslav Danyliv Sep 27 '22 at 05:39
  • Can you post a simple, small and complete reproducible sample? With few fake models with few properties and the query you are looking for. – dani herrera Sep 27 '22 at 06:35

0 Answers0