0

I have the following peices of code:

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; }
}

In my WPF XAML I have a Listbox with checkboxes. Currently ContactLocations returns all the locations. I'd like to add the AdminLocation list to the mix and set the IsEnabled flag to false for all the locations that are NOT found in AdminLocation but are in ContactLocations.

For example ContactLocations could include:

Location1 Location2 Location3 Location4 Location5

So what I'd like to see is Location4 and Location5 IsEnabled = false and all the other items would be set to true.

I have code that will eliminate Location4 and Location5 from the list but I really want those to just have a different IsEnabled flag value.

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();
4

2 回答 2

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();

Something like this would help?

于 2013-11-05T16:55:05.027 回答
1

I believe you can check the list in the existing query with 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; }
}
于 2013-11-05T16:55:11.807 回答