假設我有一個要過濾的物件串列:
using System;
using System.Collections.Generic;
using System.Linq;
public class Person
{
public string Name;
public int Age;
public List<int> Attributes;
}
class HelloWorld
{
static void Main()
{
var p1 = new Person();
p1.Name = "john";
p1.Attributes = new List<int> { 1, 2, 3, 6, 9 };
p1.Age = 25;
var p2 = new Person();
p2.Name = "steve";
p2.Attributes = new List<int> { 2, 5, 9, 10 };
p2.Age = 47;
List<Person> people = new List<Person> { p1, p2 };
people.Where(p=>p.Age > 20)
.Where(p=>p.Attributes ?????);
}
}
我將如何查詢屬性超過閾值(例如 >10)的 People 物件?似乎我需要第二個查詢,但我不知道如何嵌套它們。
uj5u.com熱心網友回復:
您可以使用Any(). 無需使用多個Where()子句,您可以將多個條件包含在一個Where()子句中
Any() :確定序列的任何元素是否存在或滿足條件。
var thresholdValue = 10;
var result = people.Where( //Filter list of people based on below conditions
p=>p.Age > 20 && //Age should be greater than 20
//**Atleast one** attribute should be greater than thresholdValue
p.Attributes.Any(x => x > thresholdValue));
我使用&&運算子是因為有問題看起來你想過濾滿足這兩個條件的人。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/339314.html
上一篇:讓你硬碟中的秘密檔案(私人學習資料)無處可藏,Python開發桌面程式——檔案搜索工具
下一篇:嘗試在Angular中使用ng-upload上傳檔案時遇到“TypeError:無法讀取未定義的屬性(讀取“模因”)
