0

I have a list thus:

List<Option> OptionList = getOptions();

an Option has various properties but the important ones are

decimal Price 
string EqtCode

I then have a second list thus:

List<string> EqtCodes

What I am trying to do is grab options from OptionList where their EqtCode matches ALL the ones in the EqtCodes list.. BUT, to make it more complicated I actually need them to be grabbed in 'sets' of ALL of the items in the EqtCodes list, picking the LOWEST price of any duplicates but grabbing them in SETs of everything in the EqtCodes list so.. to attempt to explain further

say the eqtCodes has the following in it

['A','B','C']

and the optionList has

{['A',99.99],['A',89.99],['B',20.00],['C',35.00'],['C',30.00]}

What I need is ONLY

['A',89.99],['B',20.00],['C',30.00'] 

i dont want it to grab all of the A's and B's and C's unless it gets them in lumps of 1 A, 1 B and 1 C

selected out currently I have

var whatIwant = OptionList.Where(o=>o.EqtCode.In(EqtCodes));

sadly this will grab everything, not to mention ignore the price.. likewise, if the optionlist was

  {['A',99.99],['A',89.99],['B',20.00],['C',35.00'],['C',30.00],['B',22.00],['F',33.33], ['B',19.99]}

it would need to grab

{['A',99.99],['A',89.99],['B',20.00],['C',35.00'],['C',30.00],['B',19.99]}

so its basically grabbing 'full sets' of ALL of the items in the EqtCodes list each time

I am sure LINQ could do this, but I cannot :)

as ever any help much appreciated

thanks

nat
  • 2,185
  • 5
  • 32
  • 64

2 Answers2

1

You could try this.

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();

Additionally, because you talk about its got to be all the members in EqtCodes the following code will return an empty set if not enough items match the criteria.

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];

Multi set solution

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();

Then to a single set

var singleSet = q.SelectMany(x => x); 

or

var singleSet = q.SelectMany(x => x).ToArray();
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
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]));
dav_i
  • 27,509
  • 17
  • 104
  • 136