1

I have the following data in a table:

e.g. 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. i.e you won't get something like 'Toyota, Ford'.

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

1
  • You might also check out the Tuple.Create factory method. Might clean up what you have a bit. Sep 28, 2011 at 21:49

2 Answers 2

4

This should do the trick:

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

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();
2
  • I don't think this does what the OP wants. In your example, c.Make is still going to be 'Toyota, Toyota' not just 'Toyota'
    – scottm
    Sep 28, 2011 at 21:36
  • @scottm, yeah, I think you're right. I fixed the solution so at least it's right, but it's clear you figured out what he meant before I did. +1 Sep 28, 2011 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.