[英]Finding an item in a complex object
I have a sequence of elements string[]: 我有一个元素序列string []:
new string[] { "A", "B", "C", "D" }
And there is also an object consisting of such several sequences object: 还有一个由以下几个序列对象组成的对象:
List<Test> obj = new List<Test>()
{
new Test() {Lists = new List<string>() { "A", "B" } },
new Test() {Lists = new List<string>() { "A", "C" } },
new Test() {Lists = new List<string>() { "C" } }
};
I want to find the missing element ( "D" ) in all object collections. 我想在所有对象集合中找到缺少的元素( “ D” )。 That's what I got:
那就是我得到的:
private static List<string> FindeMissingElements()
{
string nonExistentElement = null;
List<string> nonExistentElements = new List<string>();
foreach (var elemArr in arr)
{
foreach (var elemObj in obj)
{
if (elemObj.Lists.Any(a => a.Contains(elemArr)))
{
nonExistentElement = null;
break;
}
nonExistentElement = elemArr;
}
if (nonExistentElement != null)
nonExistentElements.Add(nonExistentElement);
}
return nonExistentElements;
}
I would like to simplify the code and use LINQ if possible... 我想简化代码,并在可能的情况下使用LINQ ...
//Flatten the complex object - Get all the elements from the complex object into an IEnumerable collection //Flatten the list -- //平铺复杂对象-将复杂对象中的所有元素放入IEnumerable集合//平铺列表-
var listB = (from lm in obj select lm.Lists).SelectMany(it => it.ToList()); var listB =(从obj中的lm选择lm.Lists).SelectMany(it => it.ToList());
//Below list A is input elements //use IEnumerable except extension as below - missingElements IEnumerable will have your elements from complex object -- //列表A下面是输入元素//使用IEnumerable,但扩展名如下所示-missingElements IEnumerable将从复杂对象中获取元素-
string[] listA = new string[] { "A", "B", "C", "D", "E" }; string [] listA = new string [] {“ A”,“ B”,“ C”,“ D”,“ E”}; var missinElements = listA.Except(listB);
var missinElements = listA.Except(listB);
First, I would flatten the sources into a list so I have a collection of actual values. 首先,我将这些源放到一个列表中,以便收集实际值。 To do this, it's best to use
SelectMany
( tests
is the original list, and domain
is the array of possible elements) 为此,最好使用
SelectMany
( tests
是原始列表, domain
是可能元素的数组)
var sourceElements = tests.SelectMany(test => test.Lists);
This will get the Lists
properties for each test, and join all the results together, so in your example, you'll have a result of 这将获取每个测试的
Lists
属性,并将所有结果结合在一起,因此在您的示例中,您将获得以下结果:
["A", "B", "A", "C", "C"]
You can use Distinct
to only get unique items, so the code is 您可以使用
Distinct
仅获取唯一项,因此代码为
var sourceElements = tests.SelectMany(test => test.Lists).Distinct();
Now, the only thing left to do is to find the items that are in the domain
but not in the sourceElements
, ie their set difference. 现在,剩下要做的就是找到
domain
而不是sourceElements
中的sourceElements
,即它们的集合差异。 That can easily be done with the Except
method. 这可以通过
Except
方法轻松完成。
var missing = domain.Except(sourceElements);
So, to put everything together, your method should be: 因此,将所有内容放在一起,您的方法应为:
private static IEnumerable<string> FindMissingElements(
IEnumerable<Test> tests, IEnumerable<string> domain)
{
var sourceElements = tests.SelectMany(test => test.Lists).Distinct();
var missing = domain.Except(sourceElements)
return missing;
}
I think this can help you: 我认为这可以帮助您:
var list1 = new string[] { "A", "B", "C", "D" }
var nonExistentElements = new List<string>(list1);
obj.ForEach(o => nonExistentElements = nonExistentElements.Except(o.Lists).ToList());
I Try and test your question like this: 我尝试这样测试您的问题:
private static List<string> FindeMissingElements()
{
var objectArray = new[] {"A", "B", "C", "D"};
var obj = new List<Test>()
{
new Test() {Lists = new List<string>() { "A", "B" } },
new Test() {Lists = new List<string>() { "A", "C" } },
new Test() {Lists = new List<string>() { "C" } }
};
var missingValues = objectArray.Where(x => !obj.Any(c => c.Lists.Any(v => v == x))).ToList();
return missingValues.any() ? missingValue:missingValue = new List<string>();
}
With 1 row you can find missing value. 在1行中,您可以找到缺失的值。
GoodLuck. 祝好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.