Series对象包含一个名为Skus的属性,它是IEnumerable 如果这个sku在允许的skus列表中,那么我需要那个系列。
在下面的例子中,我加入了s.SeriesId,这是不正确的。 我相信它必须是s.Skus的集合 我只想返回一个在集合中包含sku的系列。
IEnumerable<Data.Models.Series> series = await _seriesRepository.GetSeriesAsync(Properties.Settings.Default.Channel, page, limit);
string[] skusInSeries = series?.SelectMany(x => x.Skus).Distinct().ToArray();
IEnumerable<string> itemNumbers = GetAllowedSkus(Customer, Shipto, EnvironmentCode, AcceptLanguage, skusInSeries, Warehouse);
var selected = from s in series
join i in itemNumbers
on s.SeriesId equals i //s.Skus IEnumerable<string>
select s;
答案 0 :(得分:1)
var selected = from s in series
where itemNumbers.Any(i => s.Skus.Contains(i))
select s;
或者另一种方式:
var selected = from s in series
where s.Skus.Any(sku => itemNumbers.Contains(sku))
select s;
我猜通常会有Skus
多于itemNumbers
,而第一选择更好。将itemNumbers
更改为可以传递给数据库的列表也可能更好:
var itemNumbers = GetAllowedSkus(Customer, Shipto, EnvironmentCode, AcceptLanguage, skusInSeries, Warehouse).ToList();
var selected = from s in series
where itemNumbers.Any(i => s.Skus.Contains(i))
select s;
如果不涉及SQL(或其他)数据库,您可以将itemNumbers
转换为HashSet
以进行有效查找:
var itemNumbers = new HashSet<string>(GetAllowedSkus(Customer, Shipto, EnvironmentCode, AcceptLanguage, skusInSeries, Warehouse));
var selected = from s in series
where s.Skus.Any(sku => itemNumbers.Contains(sku))
select s;