选择语句中的Linq逻辑

时间:2013-11-05 16:46:56

标签: c# wpf linq

我有以下代码:

List<String> AdminLocation= new List<String>();
AdminLocation.Add("Location1");
AdminLocation.Add("Location2");
AdminLocation.Add("Location3");

AdminLocation.Cast<string>().ToList()

ContactLocations = Locations
    .Where(l => l.Active == "Y").OrderBy(l => l.Name)
    .Select(l => new Location { DbLocation = l, IsChecked = false })
    .ToList();

public class Location    {
    public db.Location DbLocation { get; set; }
    public Boolean IsChecked { get; set; }
    public Boolean IsEnabled { get; set; }
}

在我的WPF XAML中,我有一个带复选框的列表框。目前,ContactLocations返回所有位置。我想将AdminLocation列表添加到混合中,并将所有在AdminLocation中找不到但位于ContactLocations中的位置的IsEnabled标志设置为false。

例如,ContactLocations可以包括:

LOCATION1 LOCATION2 LOCATION3 LOCATION4 Location5

所以我想看到的是Location4和Location5 IsEnabled = false,所有其他项目都将设置为true。

我的代码会从列表中删除Location4和Location5,但我真的希望这些代码只有一个不同的IsEnabled标志值。

ContactLocations = Locations
    .Where(l => l.Active == "Y").OrderBy(l => l.Name)
    .Select(l => new Location { DbLocation = l, 
                                IsChecked = false, 
                                IsEnabled = true [if contains in AdminLocation] else false })
    .ToList();

2 个答案:

答案 0 :(得分:1)

ContactLocations = Locations
.Where(l => l.Active == "Y").OrderBy(l => l.Name)
.Select(l => new Location { DbLocation = l, 
                            IsChecked = false, 
                            IsEnabled = AdminLocation.Contains(l.Name) })
.ToList();

这样的事情有帮助吗?

答案 1 :(得分:1)

我相信您可以使用contains

查看现有查询中的列表
List<String> AdminLocation= new List<String>();
AdminLocation.Add("Location1");
AdminLocation.Add("Location2");
AdminLocation.Add("Location3");

AdminLocation.Cast<string>().ToList()

ContactLocations = Locations
    .Where(l => l.Active == "Y").OrderBy(l => l.Name)
    .Select(l => new Location { DbLocation = l, IsChecked = false, IsEnabled = AdminLocation.Contains(l.Name) })
    .ToList();

public class Location    {
    public db.Location DbLocation { get; set; }
    public Boolean IsChecked { get; set; }
    public Boolean IsEnabled { get; set; }
}