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

时间:2014-05-13 10:30:41

标签: 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;
        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以获取过滤后的对象,而不使用&#34; add&#34;并且&#34;得到&#34;用于创建新对象的函数:

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

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我不确定你应该走这条路,因为整个事情,通常看起来有点过于复杂。我首先尝试简化逻辑。

否则,这是ReSharper建议的(使用query syntax

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