首页 > 解决方案 > 动态使用 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;
        }
    }
}

标签: c#linq

解决方案


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

我希望你觉得这有帮助。


推荐阅读