I wonder is there a way to prevent an enum
with duplicate keys to compile?
For instance this enum
below will compile
public
public bool ValidateAllDistinct(Type enumType)
{
return !Enum.GetNames(enumType).All(outerName
=> Enum.GetNames(enumType).Any(innerName
=> innerName == outerName
? true
: Enum.Parse(enumType, innerName) != Enum.Parse(enumType, outerName)));
}
I simple test method for your unittest.
This isn't prohibited by the language specification, so any conformant C# compiler should allow it. You could always adapt the Mono compiler to forbid it - but frankly it would be simpler to write a unit test to scan your assemblies for enums and enforce it that way.
another solution to have unique values based on selected columns
var uniqueData = temp.Select(u => new tblschClassSchedule
{
TeacherName = u.TeacherName,
SubjectName = u.SubjectName,
}).Distinct() ;
this will get only 2 columns and only unique data for those columns
Unit test that checks enum and shows which particular enum values has duplicates:
[Fact]
public void MyEnumTest()
{
var values = (MyEnum[])Enum.GetValues(typeof(MyEnum));
var duplicateValues = values.GroupBy(x => x).Where(g => g.Count() > 1).Select(g => g.Key).ToArray();
Assert.True(duplicateValues.Length == 0, "MyEnum has duplicate values for: " + string.Join(", ", duplicateValues));
}
Here's a simple unit test that checks it, should be a bit faster:
[TestMethod]
public void Test()
{
var enums = (myEnum[])Enum.GetValues(typeof(myEnum));
Assert.IsTrue(enums.Count() == enums.Distinct().Count());
}