Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
C# Community
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
VB.NET
VB.NET General Discussion
Get a distinct list of values from a one property in a List (Of T)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="jmcilhinney, post: 184098, member: 641"] By the way, don't use [I]FirstOrDefault[/I] if you don't care about the object but only whether the object exists. If you were going to do it like you were, rather than this: [CODE=vbnet]findObj = distinctList.FirstOrDefault(Function(xx) xx.ValueX = itm.Prop2) If findObj Is Nothing Then[/CODE] you would do this: [CODE=vbnet]If Not distinctList.Any(Function(xx) xx.ValueX = itm.Prop2) Then[/CODE] Also, don't create objects that you never use. What is the point of this: [CODE=vbnet]Dim findObj As New OnePropObj findObj = distinctList.FirstOrDefault(Function(xx) xx.ValueX = itm.Prop2)[/CODE] You declare the variable and assign a new object to it, then you immediately discard that object and assign the result of a method to the variable. Only use the [I]New[/I] keyword if you actually want a new object. That code should have been this: [CODE=vbnet]Dim findObj As OnePropObj findObj = distinctList.FirstOrDefault(Function(xx) xx.ValueX = itm.Prop2)[/CODE] or, even better, this: [CODE=vbnet]Dim findObj = distinctList.FirstOrDefault(Function(xx) xx.ValueX = itm.Prop2)[/CODE] There's no point separating the declaration and initialisation of a variable if nothing happens in between. There's also no need to specify the type of a variable if it can be inferred from the initialising expression, although you can if you want to: [CODE=vbnet]Dim findObj As OnePropObj = distinctList.FirstOrDefault(Function(xx) xx.ValueX = itm.Prop2)[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
VB.NET
VB.NET General Discussion
Get a distinct list of values from a one property in a List (Of T)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom