c# - Find element in a list that is contained in another class sub-list -


can me more performance , more readable code piece of code?

var prd = new list<prodotti>(); foreach (var prodotto in db.prodotti)        foreach (var prdid in prdidx)                if (prdid._idprodotto == prodotto.idprodotto)                        foreach (var prodottoseriali in prodotto.allserialnumbers)                               if (prdid.serialnumber == prodottoseriali)                      if (prdid.serialnumber == prodottoseriali)                                                                 if (prd.count(c => c.idprodotto == prdid._idprodotto) == 1)                                     prd.single(c => c.idprodotto == prdid._idprodotto).serialnumbers.add(prdid.serialnumber);                                 else                                 {                                     var tmpprd = new prodotti()                                     {                                         idprodotto = prodotto.idprodotto,                                         ...                                         descrizione = prodotto.descrizione,                                     };                                        tmpprd.serialnumbers.add(prdid.serialnumber);                                     prd.add(tmpprd);                                 } 

where db.prodotti:

public partial class prodotti {     public int idprodotto { get; set; }     public list<string> serialnumbers { get; set; }     public list<string> allserialnumbers     {                 {           return cfgdb.getserialbyproduct(this.idprodotto)                   .select(c => c.serialnumber).tolist();         } 

and prdidx list of:

public class cfgprodotti_noleggio : cfgclass {     public int idseriale { get; set; }     public int _idprodotto { get; set; }     public string serialnumber { get; set; } 

i have tried translate linq without success:

var prd = db.prodotti.tolist() where(t2 => prdidx.any(t1 => t1._idprodotto== t2.idprodotto)) .tolist(); 

you want selectmany on list of prodotti make one:

var l = db.prodotti.selectmany(p => p.serialsnumbers).  

then have list of serialnumbers. make intersect of list prdidx :

var result = prdidx.select(m => m.serialnumber).intersect(l); 

the result var ienumerable of common serialnumber (call .tolist() if want list)

note : untested code

based on comments :

var result = db.prodotti.where(p => prdidx.any(s => p.serialsnumbers.contains(s.serialnumber)); 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -