从列表中选择所有匹配ID和颜色Linq选择器的SKU
我有一个使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定 Tuple 是否可以通过 Linq-to-Sql 进行翻译,但值得一试。使用本地集合对此进行了测试,因此至少它应该适用于不可 IQueryable 的集合。
Edit2:
也许沿着这些思路的东西会起作用。
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.
Edit2:
Perhaps something along these lines would work.