Bir nesne var:Listede Linq ile nesneyi nasıl bulabilirim?
public class MyObject
{
public int Id { get; set; }
public List<MyObject> Items { get; set; }
}
Ve MyObject listesi var:
List<MyObject> collection = new List<MyObject>();
collection.Add(new MyObject()
{
Id = 1,
Items = null
});
collection.Add(new MyObject()
{
Id = 2,
Items = null
});
collection.Add(new MyObject()
{
Id = 3,
Items = null
});
List<MyObject> collectionMyObject = new List<MyObject>();
collectionMyObject.Add(new MyObject()
{
Id = 4,
Items = collection
});
collectionMyObject.Add(new MyObject()
{
Id = 5,
Items = null
});
nasıl Linq ile collectionMyObject içinde Id = 2 ile nesne bulabilirim? Where
anahtar kelime ve şöyle bir lambda kullanarak
Cevabınız için teşekkürler! Ben biraz düzeltilmiş bir sorgu yaşıyorum ve çalışır (s => s.Items! = Null) .SelectMany (o => o.Items) .FirstOrDefault (io => io.Id == 2); – Alexandr
@Alexandr, null değerlerini dikkate almak için cevabı güncelledi. – Andrei
tamam, teşekkürler! :) – Alexandr