gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:29:05 25 4
gpt4 key购买 nike

我有一个嵌套对象,我必须根据更深层次过滤它。

这些是我的类(class):

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 以获取过滤后的对象,而不使用“添加”和“获取”函数来创建新对象:

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 的建议(使用 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);
}

关于c# - 用于过滤 5 个嵌套类的 Lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23628795/

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