Reputation: 259
I'm not a big fan of VB. Can anyone help me make this piece of code in c#?
Public ReadOnly Property HasErrors() As Boolean
Get
Return (Aggregate o In Me _
Let errObj = TryCast(o, IDataErrorInfo) _
Where If(errObj IsNot Nothing, errObj.Error <> Nothing, False) _
Into Count()) > 0
End Get
End Property
Update
Public MustInherit Class MyBaseCollection(Of T)
Inherits ObservableCollection(Of T)
Public ReadOnly Property HasErrors() As Boolean
Get
Return (Aggregate o In Me _
Let errObj = TryCast(o, IDataErrorInfo) _
Where If(errObj IsNot Nothing, errObj.Error <> Nothing, False) _
Into Count()) > 0
End Get
End Property
Sub New(ByVal query As IEnumerable(Of T), ByVal context As OMSEntities)
MyBase.New(query)
End Sub
End Class
Upvotes: 1
Views: 239
Reputation: 14856
It's not the exact translation, but it'll get the same result:
public bool HasErrors
{
get
{
return this.OfType<IDataErrorInfo>().Any(x => x.Error != null);
}
}
Upvotes: 0
Reputation: 122202
I'm not 100% on what this is but I think you can do it with
this.Any(o => {
var errObj = o as IDataErrorInfo;
return errObj != null && errObj.Error != null
});
or you can do the more functional style:
this.Select(o => o as IDataErrorInfo)
.Any(errObj => errObj != null && errObj.Error != null);
Upvotes: 0
Reputation: 125650
There is no equivalent for Aggregate
in syntax-based query in C#. You have to use methods.
public bool HasErrors
{
get
{
return this.Select(x => x as IDataErrorInfo)
.Where(x => x != null && x.Error != null)
.Count() > 0;
}
}
Or easier version with Count(predicate)
overload:
public bool HasErrors
{
get
{
return this.Select(x => x as IDataErrorInfo)
.Count(x => x != null && x.Error != null) > 0;
}
}
or even better with Any(predicate)
:
public bool HasErrors
{
get
{
return this.Select(x => x as IDataErrorInfo)
.Any(x => x != null && x.Error != null);
}
}
Upvotes: 1