KoolKabin
KoolKabin

Reputation: 17683

What can be Short cut Code for following

Short Code for:

    Dim NewItemList As List(Of OrderDetail) = New List(Of OrderDetail)

    For Each Item As OrderDetail In OrderInfoDetails
        If Item.Status = EnumOrderStatus.Taken Then
            NewItemList.Add(Item)
        End If
    Next

Tried:

    Dim NewItemList As List(Of OrderDetail) = From Item As OrderDetail In OrderInfoDetails Where Item.Status = EnumOrderStatus.Taken Select Item

But got a error saying cannot convert where list of orderdetail to generic list

Upvotes: 1

Views: 58

Answers (3)

sloth
sloth

Reputation: 101142

How about simply

Dim NewItemList = OrderInfoDetails.Where(Function(e) e.Status = EnumOrderStatus.Taken).ToList()

For a simple Where I prefer the method syntax. You want a List(Of OrderDetail) instead of a IEnumerable(Of OrderDetail), so you can just call the ToList() extension method.

Upvotes: 0

Moho
Moho

Reputation: 16553

Use the Where(...) method testing the Status property for equivalency to EnumOrderStatus.Taken via lambda expression, then call the ToList() method

Upvotes: 0

Botz3000
Botz3000

Reputation: 39640

Just wrap your query in a List constructor:

Dim NewItemList = New List(Of OrderDetail)(From Item As OrderDetail In OrderInfoDetails Where Item.Status = EnumOrderStatus.Taken Select Item)

Upvotes: 1

Related Questions