Первый - лучший, исключения - лучший способ сообщить пользователям класса об ошибках.
это не рекомендуется другим способом, потому что если конструктор возвращается без ошибок, это означает, что вы правильно сконструировали объект и можете использовать его где угодно
If you're providing your own comparisons, you'll need to execute the Distinct
call in .NET code. To make sure that happens, use AsEnumerable
to turn IQueryable
into IEnumerable
:
var result = myEntity.MyDomainEntity
.Where(myDomainEntity => myDomainEntity.MySpecialID > default(int))
.AsEnumerable()
.Distinct(new FooComparer());
Of course at that point you'll be pulling more data across from the database. An alternative is to group the data instead:
var result = from entity in myEntity.MyDomainEntity
where entity.MySpecialID > 0
group entity by entity.MySpecialID into groups
select groups.FirstOrDefault();
That will get you the first entity encountered with each ID (assuming my query-fu isn't failing me). That's basically what Distinct does anyway, but it's all at the database.
(Note to future readers: calling First()
makes more sense than FirstOrDefault()
, but apparently that doesn't work.)