gpt4 book ai didi

c# - 按一个或多个参数进行过滤

转载 作者:行者123 更新时间:2023-12-04 07:45:25 25 4
gpt4 key购买 nike

我有 List<Book> Books

public class Book
{
public string Name { get; set; }
public string Author { get; set; }
public string Category { get; set; }
public string Language { get; set; }
public DateTime Publication_date { get; set; }
public string ISBN { get; set; }
}
试过了,但只有在所有参数都匹配时才有效
foreach (var item in Books)
{
if ((!string.IsNullOrEmpty(name) && item.Name == name)
&& (!string.IsNullOrEmpty(author) && item.Author == author)
&& (!string.IsNullOrEmpty(category) && item.Category == category)
&& (!string.IsNullOrEmpty(language) && item.Language == language)
&& (Publication_date.HasValue && item.Publication_date == Publication_date)
&& (!string.IsNullOrEmpty(ISBN) && item.ISBN == ISBN))
{
Console.WriteLine(item);
}
}
如何通过一个或多个参数进行过滤?就像我只输入“名称”和“语言”一样,它会向我打印两个参数都匹配的书(如果字符串为空或为空,则应跳过参数)
谢谢)

最佳答案

逻辑有点复杂,但通过使用 continue 变得更简单.

foreach (var item in Books)
{
if (!string.IsNullOrEmpty(name) && item.Name != name) continue;
if (!string.IsNullOrEmpty(author) && item.Author != author) continue;
if (!string.IsNullOrEmpty(category) && item.Category != category) continue;
if (!string.IsNullOrEmpty(language) && item.Language != language) continue;
if (Publication_date.HasValue && item.Publication_Date != Publication_Date) continue;
if (!string.IsNullOrEmpty(ISBN) && item.ISBN!= ISBN) continue;

Console.WriteLine(item);
}

关于c# - 按一个或多个参数进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67221885/

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