c# - 动态使用 linq 搜索列表
问题描述
我想创建一个可以运行动态 linq 查询的函数。
我不想为每个搜索创建一个单独的 linq 查询,而是想创建一个动态函数,我可以在其中传递列名,我想搜索。
到目前为止,我的动态 linq 查询返回搜索参数中的字符串值,我无法到达列表中的 ID
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
List<Author> authors = new List<Author>
{
new Author {Id = 1, Name = "Mahesh Chand", Book = "ADO.NET Programming", Price = 49.95},
new Author {Id = 2, Name = "Neel Beniwal", Book = "Jump Ball", Price = 19.95},
new Author {Id = 3, Name = "Chris Love", Book = "Practical PWA", Price = 29.95}
};
// Add more items to the list
authors.Add(new Author { Id = 4, Name = "Jack", Book = "Graphics with GDI+", Price = 49.95});
authors.Add(new Author { Id = 5, Name = "Jack", Book = "Mastering C#", Price = 54.95});
authors.Add(new Author { Id = 6, Name = "Jack", Book = "Jumpstart Blockchain", Price = 44.95});
var nameFound = SearchColumn(authors, "Name", "Jack");
}
public class Author
{
public int Id;
public string Name;
public string Book;
public double Price;
}
public class MatchFound
{
public int Id;
public string Column;
public string MatchString;
}
private static List<MatchFound> SearchColumn(List<Author> authorList, string column, string searchCharacter)
{
var result = new List<MatchFound>();
// linq query #1
var separaList = authorList.Select(l => column).ToList();
foreach (var row in separaList)
{
if (row == searchCharacter)
{
var temp = new MatchFound()
{
Id = row.Id, // cannot reach ID, because linq Query #1 does not include ID
Column = column,
MatchString = row
};
result.Add(temp);
}
}
return result;
}
}
}
解决方案
var separaList = authorList.Select(l => column).ToList();
此代码不好,因为您正在选择要发送到功能的内容,如果您正在发送Name
将选择六次,Name
或者如果您正在发送Other
将选择六次Other
。
如果我理解正确,您可以通过使用Reflection来实现这一点,因此功能SearchColumn
将如下所示:
private static List<MatchFound> SearchColumn(List<Author> authorList, string column, string searchCharacter)
{
// linq query #1
FieldInfo fieldInfo = typeof(Author).GetField(column, BindingFlags.Public | BindingFlags.Instance);
return authorList
.Where(a => fieldInfo.GetValue(a).ToString() == searchCharacter)
.Select(a => new MatchFound
{
Id = a.Id,
Column = column,
MatchString = searchCharacter
}).ToList();
}
注意:如果您将Author
字段更改为属性,请使用GetProperty而不是GetField。
对于测试:
static void Main(string[] args)
{
List<Author> authors = new List<Author>
{
new Author {Id = 1, Name = "Mahesh Chand", Book = "ADO.NET Programming", Price = 49.95},
new Author {Id = 2, Name = "Neel Beniwal", Book = "Jump Ball", Price = 19.95},
new Author {Id = 3, Name = "Chris Love", Book = "Practical PWA", Price = 29.95}
};
// Add more items to the list
authors.Add(new Author { Id = 4, Name = "Jack", Book = "Graphics with GDI+", Price = 49.95 });
authors.Add(new Author { Id = 5, Name = "Jack", Book = "Mastering C#", Price = 54.95 });
authors.Add(new Author { Id = 6, Name = "Jack", Book = "Jumpstart Blockchain", Price = 44.95 });
var nameFound = SearchColumn(authors, "Name", "Chris Love");
Console.WriteLine($"Id : {nameFound.First().Id}");
var bookFound = SearchColumn(authors, "Book", "Mastering C#");
Console.WriteLine($"Id : {bookFound.First().Id}");
}
结果
Id : 3
Id : 5
我希望你觉得这有帮助。
推荐阅读
- android - 如何在 Exoplayer 中播放特定时间范围的音频?
- amazon-web-services - 来自容器的日志无法发送到 aws cloudwatch
- freeradius - Freeradius + Active Directory + Google Authenticator
- visual-studio-extensions - 打开 VSIX 库,自定义页面
- apache-nifi - NiFi 1.10 PutSQL 错误:查询必须类似于 INSERT
- r - 为什么转换为二进制变量0或1时输出“0”
- deployment - 在 Netlify 上部署 hugo 静态站点
- ios - 如何根据 segmentedControl 索引更改数据
- google-chrome-devtools - 银行网站如何隐藏网络电话
- maven - 通过 Jenkins 将工件存储到 Nexus 时出错