問題:如何簡化下面的代碼,因爲我正瞄準thing類中的30個不同屬性。Linq中的多列
代碼查找'thing'屬性的唯一性。
public class thing
{
public string Name { get; set; }
public string Colour { get; set; }
public string Position { get; set; }
public string Height { get; set; }
}
public List<thing> SeeIfAnyInListHaveAUniqueSingleColumn(List<thing> listOfThings)
{
// try colour
IEnumerable<IGrouping<string, thing>> thingQuery2 = from t in listOfThings
group t by t.Colour;
List<thing> listOfThingsFound = new List<thing>();
foreach (var thingGroup in thingQuery2)
{
if (thingGroup.Count() == 1)
{
foreach (thing thing in thingGroup) // there is only going to be 1
listOfThingsFound.Add(thing);
}
}
// try position
IEnumerable<IGrouping<string, thing>> thingQuery3 = from t in listOfThings
group t by t.Position;
foreach (var thingGroup in thingQuery3)
{
if (thingGroup.Count() == 1)
{
foreach (thing thing in thingGroup) // there is only going to be 1
listOfThingsFound.Add(thing);
}
}
return listOfThingsFound;
}
上http://www.programgood.net/2010/11/06/FindingUniquenessInData.aspx
是所有屬性字符串嗎? – 2010-11-07 22:10:21
你想知道任何財產是不同的?或者你是否總是在代碼中知道你正在檢查的財產? – JeremyWeir 2010-11-08 00:00:38
亨克 - 好問題!是的,所有屬性都是字符串。 – 2010-11-08 00:30:31