gpt4 book ai didi

c# - 选择对象列表中对象的属性,该对象也在另一个对象列表中

转载 作者:太空狗 更新时间:2023-10-29 22:19:27 27 4
gpt4 key购买 nike

我有下一个架构:

public class Element
{
public uint Id { get; set; }
public ICollection<ElementDetails> elementDetails { get; set; }
}
public class ElementDetails
{
public string ElementTitle { get; set; }
public string Content { get; set; }
}

还有 List<Element> someList包含数百个元素。我正在尝试获取包含特定文本(我称之为“种子”)的 ElementTitle(字符串)列表。我想要完成的是提前输入。这是我的尝试:

List<Element> suggestedElements = someList.Where(s => s.elementDetails.Any(ss => ss.ElementTitle.Contains(seed))).ToList();
List<string> suggestions = suggestedElements .SelectMany(t => t.elementDetails.Select(x => x.ElementTitle)).ToList() }); // contains all ElementTitle, including those ElementTitle that don't contain the "seed"...

如何去掉那些不包含种子的元素?

最佳答案

List<string> suggestions = someList.SelectMany(x => x.elementDetails)
.Where(y => y.ElementTitle.Contains(seed))
.Select(z => z.ElementTitle)
.ToList();

更简单:

List<string> suggestions  = someList.SelectMany(x => x.elementDetails)
.Select(y => y.ElementTitle);
.Where(z => z.Contains(seed))
.ToList();

关于c# - 选择对象列表中对象的属性,该对象也在另一个对象列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16460886/

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