0

I have a database that I'm connecting to using EF Core.

Table:

Id    Sku    Colour
--------------------
 1    123    blue
 1    124    black
 1    125    green
 2    126    yellow
 2    127    red
 3    128    white
 4    129    pink

Now I have the input list that is for example 1,blue format and I need this whole list to check in the db and return all the skus that are matching to that.

What I have so far but only works one at a time because I can’t get it to work from a list of items to return a list of corresponding skus.

public async Task<List<ProdBarcode>> FindSku(string id, string colour)
{
    return await ProdBarcode.Where(x => x.ID == id 
                                        && x.Colour == colour)
                            .ToListAsync();
}

How ca I return a list with multiple Skus per item+colour combinations? I have tried instead of the 2 param id and colour to have a list of strings as an input but then all the modifications I did on the return are not working also not too sure what to use maybe contains? Current method will cause a webpage to throw a connection time out as it takes some time to go through 2000 items at a one by one basis.

I guess the SQL will be something like:

SELECT sku 
FROM ProdBarcode 
WHERE id + ‘,’ + colour IN (inputList)
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
QuickSilver
  • 730
  • 5
  • 28

1 Answers1

0

Not sure if a Tuple can be translated via Linq-to-Sql, but its worth a test. Tested this with a local collection, so at the very least it should work on a set that is not IQueryable.

var searchCriteria = new List<Tuple<string, string>>()
{
    new Tuple<string, string>("1", "blue"),
    new Tuple<string, string>("2", "blue"),
    new Tuple<string, string>("3", "red"),
    new Tuple<string, string>("3", "green")
};

var results = dataTable
    .Where(item => searchCriteria.Contains(new Tuple<string, string>(item.Id, item.Color)))
    .ToList();

Edit2:

Perhaps something along these lines would work.

var results = dataTable
    .Where(item => searchCriteria
        .Any(criteria =>
            criteria.Item1 == item.Id &&
            criteria.Item2 == item.Color
        )
    )
    .ToList();
hijinxbassist
  • 3,667
  • 1
  • 18
  • 23
  • I tried your approach but the id and colour needs to come from user input and needs to form a unique string to get the right data. If I do individual I might run into a situation where the id is ok but the colour for that id is not the one I was looking for as one id can have multiple colour so I can search for id 1 colour black and then search for id 2 but colour can be black and red and want to get the sku for just the red if that makes sense. – QuickSilver Mar 31 '22 at 16:30
  • I think I have a solution, but currently cannot test if it translates in Linq to Sql. I will update my answer. – hijinxbassist Mar 31 '22 at 16:35
  • Thanks I will do some more tests but at the moment it returns 0 results. – QuickSilver Mar 31 '22 at 16:37
  • I think all i need is a translation of the query at the very end to linq selector as it works fine in sql. The solution above is not working with what I have so far. Thanks for the help so far. – QuickSilver Mar 31 '22 at 17:11
  • Added one more possible solution. – hijinxbassist Mar 31 '22 at 17:42
  • I don’t have a groupby in my query I don’t think is necessary as I don’t need to reduce the list only create a unique key to search in a table so in the tabel I need to create maybe in memory a key from id plus colour to be as unique as possible to get back the right sku for that id colour combination. – QuickSilver Mar 31 '22 at 17:46
  • Updated the 2nd solution in my answer using `Any` on the search criteria. – hijinxbassist Mar 31 '22 at 20:17
  • Still not working the string done not contain a definition for Item1 or Item2. – QuickSilver Apr 01 '22 at 08:59
  • You would need to use a structure that has both the id and color values. It can be a 2d array, collection of struct/class that has those properties, or a tuple as shown in my example. – hijinxbassist Apr 05 '22 at 22:25