0

我想知道分号后的一个 sourceList 值是否包含在由 Name 属性标识的 targetList 中。如果那是真的,则必须返回布尔值。

它不工作,但我尝试了一些东西......

我想我的 linq 不正确。所有的东西......

“FoundSomething”包含在 targetList 中,因此它应该返回 TRUE。

var sourceList = new String[]
{ 
    "1;HideButton",
    "2;ShowButton",
    "3;HideButton",
    "4;ShowButton",
    "5;HideButton",
    "6;ShowButton",
    "7;HideButton",
    "8;ShowButton",
    "9;HideButton",
    "10;FoundSomething",
};

var targetList = new List<GlobalConfiguration>()
{
    new GlobalConfiguration{ Name = "444" },
    new GlobalConfiguration{ Name = "fdsdffd" },
    new GlobalConfiguration{ Name = "44" },
    new GlobalConfiguration{ Name = "fsdd" },
    new GlobalConfiguration{ Name = "fs4rtref" },
    new GlobalConfiguration{ Name = "ftrtras" },
    new GlobalConfiguration{ Name = "FoundSomething" },
};

Boolean exists = sourceList.Any(a => targetList.All(c => c.Name == a.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Last()));
4

3 回答 3

1

我不是 100% 确定我理解你的目标,但这是我最好的猜测:

sourceList
    .Select(x => x.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Last())
    .Intersect(targetList.Select(x => x.Name))
    .Any();

它的作用是创建两个序列

  1. 后半部分sourceList(分号后)
  2. 名称属性targetList

Select.

然后它用于Intersect返回两个序列共有的任何项目。

最后,它用于Any确定是否返回了任何东西Intersect(我认为这很容易理解)。

于 2012-11-28T20:23:29.353 回答
0

看起来你想要另一个Any而不是All

bool exists = sourceList
    .Select(s => s.Split(new[] { ';' }, StringSplitOption.RemoveEmptyEntries).Last())
    .Any(v => targetList.Any(t => s == t.Name));
于 2012-11-28T20:29:04.013 回答
0

你基本上有两个集合,你想看看他们的交集是不是空的。您应该这样做,这要归功于方便的Intersect扩展方法。

var source = sourceList.Select(s=>s.Split(';')[1]);
var target = targetList.Select(t=>t.Name);

return source.Intersect(target).Count() > 0;
于 2012-11-28T20:34:12.970 回答