繁体   English   中英

选择语句中的Linq逻辑

[英]Linq logic in the select statement

我有以下代码:

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可能包括:

位置1位置2位置3位置4位置5

所以我想看到的是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();
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();

这样的事情会有所帮助吗?

我相信您可以使用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; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM