我有一个清单:
List<Option> OptionList = getOptions();
一个选项有各种属性,但重要的是
decimal Price
string EqtCode
然后我有第二个清单:
List<string> EqtCodes
我想要做的是从OptionList中获取选项,其中EqtCode与EqtCodes列表中的 ALL 匹配。但是,为了使它更复杂,我实际上需要它们被抓住'设置EqtCodes列表中的所有项目,选择任何重复项的最低价格,但在EqtCodes列表中的所有内容的SET中抓取它们 所以...试图进一步解释
说eqtCodes中有以下内容
['A','B','C']
并且optionList有
{['A',99.99],['A',89.99],['B',20.00],['C',35.00'],['C',30.00]}
我只需要
['A',89.99],['B',20.00],['C',30.00']
我不希望它抓住所有的A,B和C,除非它得到1 A,1 B和1 C的块
选中了 目前我有
var whatIwant = OptionList.Where(o=>o.EqtCode.In(EqtCodes));
遗憾的是,这将抓住一切,更不用说忽略价格..
同样,如果选项列表是
{['A',99.99],['A',89.99],['B',20.00],['C',35.00'],['C',30.00],['B',22.00],['F',33.33], ['B',19.99]}
需要抓住
{['A',99.99],['A',89.99],['B',20.00],['C',35.00'],['C',30.00],['B',19.99]}
因此它每次都基本上抓住了EqtCodes列表中所有项目的“全套”
我确信LINQ可以做到这一点,但我不能:)。
一如既往的任何帮助
感谢
答案 0 :(得分:1)
你可以试试这个。
var query = from o in options
where EqtCodes.Contains(o.EqtCode)
group o by o.EqtCode into g;
select g.OrderBy(x => x.Price).First();
此外,因为你说它必须是EqtCodes中的所有成员,如果没有足够的项符合条件,下面的代码将返回一个空集。
var query = from o in options
where EqtCodes.Contains(o.EqtCode)
group o by o.EqtCode into g;
select g.OrderBy(x => x.Price).First();
var options = query.ToArray();
options = query.Length == EqtCodes.Length ? options : new options[0];
多重解决方案
var q = from o in options
where EqtCodes.Contains(o.EqtCode)
group o by o.EqtCode into g
from i in Enumerable.Range(0, options.Count / EqtCodes.Count)
let a = new {index = i, value = g.OrderBy(x => x.Price).Skip(i).Take(1)}
where a.value.Any ()
group a by a.index into b
where b.Count() == EqtCodes.Count()
select b.SelectMany(x => x.value).ToArray();
然后到一套
var singleSet = q.SelectMany(x => x);
或
var singleSet = q.SelectMany(x => x).ToArray();
答案 1 :(得分:0)
var eqtCodes = new [] {'a', 'b', 'c'};
var optionList = new []
{
new [] { 'a', '1' },
new [] { 'a', '2' },
new [] { 'b', '1' },
new [] { 'c', '1' },
new [] { 'd', '1' }
};
var lessOptionList = from o in optionList
group o by o[0] into p
select new { first = p.First() };
var result = lessOptionList.Where(a => eqtCodes.Contains(a.first[0]));