简体   繁体   English

查找清单的值 <object> 使用C#和linq导致产生真实条件

[英]Find value of list<object> that cause to make true condition using C# and linq

I have a list of objects which name is ObjectA and another list of Objects that name is ObjectB like: 我有一个名字是对象的列表ObjectA和对象名称的另一个列表是ObjectB ,如:

var result = objectA.any(x=> objectB.contain(x.item));

Now my question is: if my condition was true, how can i find value of ObjectB which caused that made my condition true? 现在我的问题是:如果我的条件为真,那么我如何找到导致我的条件为真的ObjectB值?

you can try is: 您可以尝试的是:

List<ObjectB> sample = new List<ObjectB>();
List<ObjectA> mainList = new List<ObjectA>();
objectB result = sample.FirstOrDefault(ele => mainList.Any(x => ele == x.item);

or If you want Multiple Objects then :- 或者,如果您想要多个对象,则:-

  List<objectB> resultList = sample.Where(ele => mainList.Any(x => ele == x.item).ToList();

I assumed that ObjectB is a representation of list 我假设ObjectB是列表的表示

A whole plethora of findy routines for your amusement 大量的娱乐例程供您娱乐

Enumerable.FirstOrDefault Method (IEnumerable) Enumerable.FirstOrDefault方法(IEnumerable)

Returns the first element of a sequence, or a default value if the sequence contains no elements. 返回序列的第一个元素,如果序列不包含任何元素,则返回默认值。

var obj = objectA.FirstOrDefault(x=> objectB.Contains(x.item));

Enumerable.First Method (IEnumerable) Enumerable.First方法(IEnumerable)

Returns the first element of a sequence. 返回序列的第一个元素。

var obj = objectA.First(x=> objectB.Contains(x.item));

Enumerable.Where Method (IEnumerable, Func) Enumerable.Where方法(IEnumerable,Func)

Filters a sequence of values based on a predicate. 根据谓词过滤值序列。 Each element's index is used in the logic of the predicate function. 每个元素的索引都在谓词功能的逻辑中使用。

var objs = objectA.Where(x=> objectB.Contains(x.item));

List.IndexOf Method (T) List.IndexOf方法(T)

Searches for the specified object and returns the zero-based index of the first occurrence within the entire List. 搜索指定的对象,并返回整个List中第一次出现的从零开始的索引。

var obj = objectA.IndexOf(obj);

Enumerable.Last Method (IEnumerable) Enumerable.Last方法(IEnumerable)

Returns the last element of a sequence 返回序列的最后一个元素


Enumerable.LastOrDefault Method Enumerable.LastOrDefault方法

Returns the last element of a sequence, or a default value if no element is found. 返回序列的最后一个元素,如果找不到元素,则返回默认值。


Enumerable.Intersect Method (IEnumerable, IEnumerable) Enumerable.Intersect方法(IEnumerable,IEnumerable)

Produces the set intersection of two sequences by using the default equality comparer to compare values. 通过使用默认的相等比较器比较值来产生两个序列的集合相交。

int[] id1 = { 44, 26, 92, 30, 71, 38 };
int[] id2 = { 39, 59, 83, 47, 26, 4, 30 };

IEnumerable<int> both = id1.Intersect(id2);

foreach (int id in both)
    Console.WriteLine(id);

/*
 This code produces the following output:

 26
 30
*/

Enumerable.Except Method (IEnumerable, IEnumerable ) Enumerable.Except方法(IEnumerable,IEnumerable

Produces the set difference of two sequences by using the default equality comparer to compare values. 通过使用默认的相等比较器比较值来产生两个序列的集合差异。

double[] numbers1 = { 2.0, 2.0, 2.1, 2.2, 2.3, 2.3, 2.4, 2.5 };
double[] numbers2 = { 2.2 };

IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2);

foreach (double number in onlyInFirstSet)
    Console.WriteLine(number);

/*
 This code produces the following output:

 2
 2.1
 2.3
 2.4
 2.5
*/

Be careful with iterating the IEnumerable multiple times. 多次迭代IEnumerable要小心。

This is, performance and simplicity wise, a nice approach for you. 从性能和简便性的角度来看,这是一种不错的选择。

var result = objectA.Where(x => objectB.contain(x.item)).ToList();

if (result.Any())
{
    // use the already enumerated objects inside "result" list
}

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

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