从列表中选择所有匹配ID和颜色Linq选择器的SKU

发布于 2025-01-18 03:25:56 字数 926 浏览 2 评论 0原文

我有一个使用 EF Core 连接到的数据库。

表:

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

现在我有输入列表,例如 1,blue 格式,我需要整个列表来检查数据库并返回与其匹配的所有 sku。

到目前为止,我所拥有的但一次只能运行一个,因为我无法让它从项目列表中运行以返回相应 sku 的列表。

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

如何返回每个商品+颜色组合包含多个 SKU 的列表?我尝试过使用字符串列表作为输入,而不是使用 2 个参数 id 和颜色,但是我在返回时所做的所有修改都不起作用,也不太确定该使用什么,也许 contains?当前的方法将导致网页抛出连接超时,因为逐一浏览 2000 个项目需要一些时间。

我猜 SQL 会是这样的:

SELECT sku 
FROM ProdBarcode 
WHERE id + ‘,’ + colour IN (inputList)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾挽 2025-01-25 03:25:57

不确定 Tuple 是否可以通过 Linq-to-Sql 进行翻译,但值得一试。使用本地集合对此进行了测试,因此至少它应该适用于不可 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:

也许沿着这些思路的东西会起作用。

var results = dataTable
    .Where(item => searchCriteria
        .Any(criteria =>
            criteria.Item1 == item.Id &&
            criteria.Item2 == item.Color
        )
    )
    .ToList();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文