我有下一个arhitecture:
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"...
如何摆脱那些不含种子的元素?
答案 0 :(得分:12)
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();