gpt4 book ai didi

c# - 从 ObservableCollection 中的 ObservableCollection 中的 Linq

转载 作者:行者123 更新时间:2023-11-30 23:11:57 26 4
gpt4 key购买 nike

我正在尝试获取值“thisValueIwant”。有没有可能如此容易地获得这个值(value)?或者也许这两个 ObservableCollection 有另一种解决方案

public class Foo
{
public int number { get; set; }
public ObservableCollection<FooInFoo> Details { get; set; }
}

public class FooInFoo
{
public string thisValueIwant { get; set; }
}

public class TestClass
{
public void Test()
{
ObservableCollection<Foo> FooCollection = new ObservableCollection<Foo>();

FooCollection.Add(new Foo{
number =1,
Details = new ObservableCollection<FooInFoo>{ new FooInFoo {thisValueIwant = "very important string"}}
});

string x = (from f in FooCollection
where f.number == 1
select ???)
}
}

最佳答案

ObservableCollection<FooInFoo> Details是一个集合,您必须决定您想要哪些详细信息:第一个、最后一个、任何一个或所有。

假设您想要第一个:

var d = FooCollection.Where(f => f.Number == 1).FirstOrDefault()?.Details.FirstOrDefault()?.thisValueIwant;

或者最后一个:

var d = FooCollection.Where(f => f.Number == 1).FirstOrDefault()?.Details.LastOrDefault()?.thisValueIwant;

或全部(具体化为数组):

var ds = FooCollection.Where(f => f.Number == 1).FirstOrDefault()?.Details.Select(d => d.thisValueIwant).ToArray();

关于c# - 从 ObservableCollection 中的 ObservableCollection 中的 Linq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44408174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com