2016-11-11 112 views
1

我试图使用DISTINCT我的名单上stationList但好像我做错了什么,因为我没有得到明显的列表。我将如何获得Value中不同的地方,我在那里设置stationList获取不同的名称

foreach (StationCategory stationCategory in productCatalog.Programming.StationCategory) 
    { 
     StringBuilder stationList = new StringBuilder(); 
     foreach (Station station in stationCategory.Station.Distinct()) 
     { 
      stationList.Append(station.StationName + ","); 
     } 
     offer.FeatureList.Add(new Feature() { FeatureName = "<b>" + stationCategory.CategoryName + "</b>", Value = stationList.ToString().TrimEnd(',') }); 
    } 
+1

你没有得到什么?什么是“Station”,它包含什么,以及你如何比较平等?请发布*相关*代码,您发布的内容显示了对类别的迭代。邮政编码实际上重现问题 –

+0

以应用不同,您需要一个标准来定义独特性。看看这个[post](http://stackoverflow.com/questions/10255121/get-a-list-of-distinct-values-in-list)。我建议尝试 –

+0

后的“GroupBy”回答如果工作站可以在不同类别中重复,则需要用一个productCatalog.Programming.StationCategory.SelectMany替换2个'foreach'循环(sc => sc.Station ).Distinct()' – KMoussa

回答

4

您当前的代码的工作,但它需要在不同的对象引用,而不是你所期望的名字。要做到这一点,你必须告诉代码如何匹配Distinct

这里有一个问题,虽然,因为Distinct不采取拉姆达,所以你不能说“给我不同的站名。为了做到这一点,你必须组,这样的样品中:

var ds = stationCategory.Station.GroupBy(s => s.StationName) 
           .Select(g => g.FirstOrDefault()); 

ds现在持有站的枚举。你可以从中取名。其他属性不保证是唯一的。 (如果你只在名称感兴趣,你可以使用这个Select.Select(g => g.FirstOrDefault()?.StationName);

0

我假定这是因为Distinct函数比较所述对象的引用不是内容,因为这是默认的相等比较

从序列

返回不同元件通过使用默认的相等比较器来比较值。

https://msdn.microsoft.com/en-us/library/bb348436(v=vs.110).aspx

您可以让您的Station实现IEqualityComparer<Station> inferface或实施IEqualityComparer

0

更新此代码以现有的一个:

foreach (StationCategory stationCategory in productCatalog.Programming.StationCategory) 
    { 
     StringBuilder stationList = new StringBuilder(); 
     foreach (var stationName in stationCategory.Station.GroupBy(x => x.Name).Select(x => x.First())) 
     { 
      stationList.Append(stationName + ","); 
     } 
     offer.FeatureList.Add(new Feature() { FeatureName = "<b>" + stationCategory.CategoryName + "</b>", Value = stationList.ToString().TrimEnd(',')}); 
    } 
+1

你有什么改变?这对OP有什么帮助?他做错了什么? –

+0

检查这个新的编辑。我相信它会起作用 –

+2

不幸的是,这个版本只会保存“Station”类的全名。请看[编辑历史](http://stackoverflow.com/posts/40543678/revisions)Ivan Stoev的编辑使其工作。在这里:'stationList.Append(stationName +“,”);''stationName'不是一个'string',而是一个'Station' –

0

.Distinct检查由于GetHashCodeEquals方法的对象的平等。如果您没有在Station类上覆盖它,它将永远不会执行正确的过滤器。

如果您已覆盖GetHashCodeEquals方法Station类,那么您应该修复它们,因为它们没有返回正确的值。

另一种方式 - 无覆盖是收集List<string>这将包含你的站名,并呼吁它.Distinct如下:

foreach (StationCategory stationCategory in productCatalog.Programming.StationCategory) 
{ 
    var stationList = new List<string>(); 
    foreach (Station station in stationCategory.Station) 
    { 
     stationList.Add(station.StationName); 
    } 
    offer.FeatureList.Add(new Feature() 
    { 
     FeatureName = "<b>" + stationCategory.CategoryName + "</b>", 
     Value = string.Join(", ", stationList.Distinct()) 
    }); 
} 
3

如果重写你的代码将工作在车站类的equals方法如下

public override bool Equals(object obj) 
{ 
    return StationName == ((Station)obj).StationName; 
} 

否则它会比较整个站对象与另一个站对象,它会给出不正确的结果。另一种方法是你可以采取的站名列表中distincts像stationCategory.Station.Select(x=>x.StationName).Distinct()或使用GroupBy

例如:

stationList.Append(string.Join(",", 
        stationCategory.Station.GroupBy(b=>b.StationName).Select(g=>g.Key).ToArray())); 
+2

@PatrickHofman更新了,SO的时间有限。同意我没有说清楚。感谢您的提醒。 – Damith

+1

欢迎........ –