简体   繁体   English

我如何使用Linq方法语法获得独特的结果

[英]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: 我需要将此数据放在以下类型的List<Tuple<int, string>> carList以便列表中的结果如下所示:

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. 如果您希望通过linq查询来完成全部操作,则应该可以使用。

If you're just looking to fix your pseudocode, then scottm's solution should work. 如果您只是想修复您的伪代码,那么scottm的解决方案应该可以使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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