C# 用于筛选5个嵌套类的Lambda表达式
我有一个嵌套对象,我必须根据更深层次对其进行过滤 这些是我的课程:C# 用于筛选5个嵌套类的Lambda表达式,c#,lambda,nested-loops,C#,Lambda,Nested Loops,我有一个嵌套对象,我必须根据更深层次对其进行过滤 这些是我的课程: class Library { public string Name { get; set; } public IList<Section> Sections { get; set; } public Library (string name, IList<Section> sections = null) { Name = name; Se
class Library {
public string Name { get; set; }
public IList<Section> Sections { get; set; }
public Library (string name, IList<Section> sections = null) {
Name = name;
Sections = sections ?? new List<Section>();
}
}
class Section {
public string Name { get; set; }
public IList<Book> Books { get; set; }
public Section(string name, IList<Book> books = null) {
Name = name;
Books = books ?? new List<Book>();
}
}
class Book {
public string Name { get; set; }
public IList<Word> Words { get; set; }
public Book(string name, IList<Word> words = null) {
Name = name;
Words = words ?? new List<Word>();
}
}
class Word {
public string Name { get; set; }
public IList<Character> Characters { get; set; }
public Word(string name, IList<Character> characters = null) {
Name = name;
Characters = characters ?? new List<Character>();
}
}
class Character {
public char chrctr { get; set; }
public Character(char character){
chrctr = character;
}
}
我想用lambda表达式替换嵌套的foreach以获取过滤对象,而不使用“add”和“get”函数创建新对象:
var filteredLibrary = new Library("filteredLibrary");
foreach (var section in library.Sections)
{
foreach (var book in section.Books)
{
foreach (var word in book.Words)
{
var chars = word.Characters.Where(c => c.chrctr == 'd').ToList();
if (chars.Count > 0)
{
filteredLibrary.Sections.Add(section); //if it doesn't exist
filteredLibrary.GetSection(section.Name).Books.Add(book); //if it doesn't exist
filteredLibrary.GetSection(section.Name).GetBook(book.Name).Words.Add(chars); //if it doesn't exist
}
}
}
}
我怎样才能做到这一点呢?我不确定你是否应该走这条路,因为总体而言,整个事情看起来有点太复杂了。我会先尝试简化逻辑 否则,下面是ReSharper建议的(使用)
你能给我举个例子吗?你的嵌套的
foreach
有效吗?在我看来,您正在将一个现有的、未过滤的部分(它仍然拥有所有的书籍)添加到“过滤库”中,然后将所需的书籍重新添加到其中。意图很清楚(我认为),但是将代码直接转换为Linq不会得到您需要的结果。另外,最后一行不应该结束.Words.Add(word)
,然后再将字符添加到单词中吗?这段代码就是一个例子,它不起作用。我的想法是使用lambda表达式获取过滤对象,而不使用“add”和“get”函数创建新对象
var filteredLibrary = new Library("filteredLibrary");
foreach (var section in library.Sections)
{
foreach (var book in section.Books)
{
foreach (var word in book.Words)
{
var chars = word.Characters.Where(c => c.chrctr == 'd').ToList();
if (chars.Count > 0)
{
filteredLibrary.Sections.Add(section); //if it doesn't exist
filteredLibrary.GetSection(section.Name).Books.Add(book); //if it doesn't exist
filteredLibrary.GetSection(section.Name).GetBook(book.Name).Words.Add(chars); //if it doesn't exist
}
}
}
}
var filteredLibrary = new Library("filteredLibrary");
foreach (var section in
from section in library.Sections
from book in section.Books
from word in book.Words
let chars = word.Characters
.Where(c => c.chrctr == 'd')
.ToList()
where chars.Count > 0
select section)
{
//if it doesn't exist
filteredLibrary.Sections.Add(section);
filteredLibrary.GetSection(section.Name).Books.Add(book);
filteredLibrary.GetSection(section.Name).GetBook(book.Name).Words.Add(chars);
}