用于过滤5个嵌套类的Lambda表达式

本文关键字:Lambda 表达式 嵌套 过滤 5个 用于 | 更新日期: 2023-09-27 18:03:09

我有一个嵌套的对象,我必须基于更深层次对它进行过滤。

这些是我的类:

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;
    }
}

这是我的对象:

var char1 = new Character('a');
var char2 = new Character('b');
var char3 = new Character('c');
var char4 = new Character('d');
var char5 = new Character('e');
var word1 = new Word("word1", new Character[] { char1 });
var word2 = new Word("word2", new Character[] { char1, char2 });
var word3 = new Word("word3", new Character[] { char1, char2, char3 });
var word4 = new Word("word4", new Character[] { char1, char2, char3, char4 });
var word5 = new Word("word5", new Character[] { char1, char2, char3, char4, char5 });
var book1 = new Book("book1", new Word[] { word1 });
var book2 = new Book("book2", new Word[] { word1, word2 });
var book3 = new Book("book3", new Word[] { word1, word2, word3 });
var section1 = new Section("section1", new Book[] { book1, book2 });
var section2 = new Section("section2", new Book[] { book1, book2, book3 });
var library = new Library("library1", new Section[] { section1, section2 });

我想用lambda表达式替换我的嵌套foreach来获得过滤对象,而不使用"add"answers"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
            }
        }
    }
}

我该怎么做?

用于过滤5个嵌套类的Lambda表达式

我不确定您是否应该走这条路,因为整个事情通常看起来有点太复杂了。我会先简化逻辑。

否则,以下是ReSharper的建议(使用查询语法)

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);
}