简体   繁体   中英

select multiple objects from list based on a where condition

I am have a list of type PriceDetails (shown below). I also have a list which can contain multiple PriceDetails objects with the same ISIN. For a given ISIN I would like to select all the PriceDetails object with that ISIN.

I thought something like below would work but it doesn't even compile.

Class

class PriceDetails
{
     string ISIN;
     string Sedol;
     double Price;
     string Source;
}

Code

    List<PriceDetails> secPrices = (from p in pList
                                    where p.ISIN == someISIN
                                    select secPrices).ToList(); 

Error message

Cannot implicitly convert type System.Collections.Generic.List<System.Collections.Generic.List<MyProg.PriceDetails> to System.Collections.Generic.List<MyProg.PriceDetails>

You need to select p instead of secPrices :

List<PriceDetails> secPrices = (from p in pList
                                where p.ISIN == someISIN
                                select p).ToList(); 

Your select is wrong, try that:

 var secPrices = (from p in pList
                                    where p.ISIN == someISIN
                                    select p).ToList(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM