简体   繁体   中英

How would I achieve a unique result using the Linq method syntax

I have the following data in a table:

eg data

0, 'Ford'    
1, 'Toyota, Toyota'  
2, 'BMW'  
3, 'Porsche, Porsche'  
4, 'BMW'

I need to place this data in the following type List<Tuple<int, string>> carList so that the results within my list would appear as follows:

0, 'Ford'    
1, 'Toyota'  
2, 'BMW'  
3, 'Porsche'  
4, 'BMW'

using the following pseudo code:

while (SQLiteDataReader.Read())  
{  
    carList.Add  
    (  
        new Tuple<int, string> (   
                                 SQLiteDataReader.GetInt32(0) ,   
                                 SQLiteDataReader.GetString(1).[Some extension method to produce unique items]
                               );
    )  
}

Note, when there are items with duplication (Toyota, Porsche) , the duplication will always be the same name. ie you won't get something like 'Toyota, Ford'.

Is there some extension method that would remove the duplication part?

这应该可以解决问题:

SQLiteDataReader.GetString(1).Split(',').First().Trim()

If you're looking to do the whole thing through a linq query, this should work.

If you're just looking to fix your pseudocode, then scottm's solution should work.

LinqDataContext db = new LinqDataContext();
List<Tuple<int, string>> results = 
   db.Cars.AsEnumerable()
          .Select(c => Tuple.Create(c.id, c.Make.Split(',')[0].Trim()))
          .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