开发者

Simplifying multiple for each loops in LINQ

in my prev question i was suggested with LINQ solution. It was great and simple. I tried to solve next similar but a bit diff problem with similar approach but i failed.

how can i make the below code better

    For Each Item As ListViewItem In R开发者_如何学GooomsListView.Items
        For Each Item1 As Room In myBookedRooms
            If Item1.UIN = Item.SubItems(1).Text Then
                Item.Checked = True
            End If
        Next
    Next


Sorry - can read but not write VB...

In C# Linq, your query would be something like:

var query = from room in RoomsListView.Items
    from bookedRoom in myBookedRooms 
    where ((Room)bookedRoom).UIN == room.SubItems(1).Text
    select room;

foreach (var room in query)
{
    room.Checked = true;
}


You can use Join for that. Here is a C# sample:

var itemsToUpdate = RoomsListView.Items
                        .Cast<ListViewItem>()
                        .Join(myBookedRooms,
                                   Item => Item.SubItems(1).Text,
                                   Item1 => Item1.UIN,
                                   (Item, Item1) => Item);

foreach (var item in itemsToUpdate)
    item.Checked = true;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜