Kri*_*ekk 3 linq list windows-phone-7
或许难以理解,但让我解释一下.我有一个List- Channel对象,都有一个ChannelId属性(int).我也有一个不同的List(int) - SelectedChannelIds,它包含ChannelId-s 的子集.
我想选择(通过LINQ?)所有- 在第二个Channel中具有ChannelId匹配的属性的对象List.
换句话说,我有以下结构:
public class Lists
{
public List<Channel> AllChannels = ChannelController.GetAllChannels();
public List<int> SelectedChannelIds = ChannelController.GetSelectedChannels();
public List<Channel> SelectedChannels; // = ?????
}
public class Channel
{
// ...
public int ChannelId { get; set; }
// ...
}
Run Code Online (Sandbox Code Playgroud)
关于LINQ查询会是什么样子的任何想法?还是有更有效的方法?我正在编写Windows Phone 7,fyi.
您可以List.Contains在Where子句中使用:
public Lists()
{
SelectedChannels = AllChannels
.Where(channel => SelectedChannelIds.Contains(channel.ChannelId))
.ToList();
}
Run Code Online (Sandbox Code Playgroud)
需要注意的是,如果你使用它会更有效率HashSet<int>,而不是List<int>为SelectedChannelIds.更改为a HashSet将改善从O(n 2)到O(n)的性能,但如果您的列表总是非常小,则这可能不是一个重要问题.