简体   繁体   English

如何使用下拉列表值(String)来过滤linq结果?

[英]How can I use drop down list value (String) to filter linq results?

I'm filling a drop-down list using the following: 我正在使用以下内容填写下拉列表:

var columnNames = db.Mapping.MappingSource.GetModel(typeof(StaffDirectoryDataContext))
                                    .GetMetaType(typeof(Person)).DataMembers;

I'm then converting that to a List<String> to populate a drop down list. 然后我将其转换为List<String>以填充下拉列表。

I then want to be able to get a set of results based on the user's selection. 然后,我希望能够根据用户的选择获得一组结果。 For example, if they select "First name" from the drop down list and type "Bob" into the text box I want to run a LINQ query where first name = bob. 例如,如果他们从下拉列表中选择“First name”并在文本框中键入“Bob”,我想运行LINQ查询,其中first name = bob。

I'm probably being thick but I can't find a way! 我可能很厚但我找不到办法! Pseudo code would be... 伪代码会......

var q = from x in dc.Persons
        where x.[selected column name] == [textbox value]
        select x;

Can anybody help? 有人可以帮忙吗? Essentially I have the column name as a String value, and I can't figure out how to tell the LINQ query that that's the column to filter on! 基本上我有列名作为字符串值,我无法弄清楚如何告诉LINQ查询这是要过滤的列!

Could do this in ADO.NET with my eyes closed, but determined to use LINQ all the way!! 我可以闭着眼睛在ADO.NET中做到这一点,但决定一直使用LINQ !!

Thanks in advance. 提前致谢。

David Buchanan has posted a solution for this problem using reflection : David Buchanan使用反射发布了此问题的解决方案:

msdn forum msdn论坛

I'm not sure you can do this dynamically, but you can do it conditionally. 我不确定你能动态地做到这一点,但你可以有条件地做到这一点。 Something like this: 像这样的东西:

switch(selected column name)
{
    case "student_no":
       q = q.where(p=>p.StudentNo == value);
       break;

    case "student_id":
       q = q.where(p=>p.StudentId == value);
       break;
}

You can iterate through your columns and keep building the wheres. 您可以遍历列并继续构建数据。 The SQL won't be executed as long as none of the calls force the IQueryable to execute. 只要没有任何调用强制执行IQueryable,就不会执行SQL。

I think expression trees are the right way to do this, but I don't know them very well so I'm going to give you the alternate way I would have done this if I didn't feel like learning expression tree building.. 我认为表达树是正确的方法,但我不太了解它们,所以如果我不想学习表达树构建,我会给你另类的方法。

public interface IFilter { IEnumerable RetreiveFilter(string filterValue); }

public class FirstNameFilter : IFilter
{
    private const string FILTER_TYPE_NAME = "First Name";
    public IEnumerable RetreiveFilter(string filterValue)
    {
        return _myData.Where(person => person.FirstName = filtervalue);
    }

    public override string ToString()
    {
        return FILTER_TYPE_NAME;
    }
}

Create a class like this for each filter type, and then fill your dropdown with these filters, and when they type info into the filter text, it will execute against the ((IFilter)filterDropDown.SelectedItem).RetreiverFilter(filterTextBox.Text); 为每个过滤器类型创建一个这样的类,然后用这些过滤器填充你的下拉列表,当他们在过滤器文本中键入info时,它将针对((IFilter)filterDropDown.SelectedItem).RetreiverFilter(filterTextBox.Text);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用jquery设置aspnet下拉列表的选定值 - How can I use jquery to set the selected value of aspnet drop down list 如何根据其中的列表值使用linq过滤列表? - How can I filter a List with linq based on a value of a list within? 如何将字符串与linq中的“过滤器”列表进行比较? - How can I compare a string to a “filter” list in linq? 如何动态地使用linq查询的结果填充下拉列表? - How can I fill a drop down list with the result of a linq query dynamically? 在Blazor中,如何使用下拉值过滤HTML - In Blazor how can I filter an HTML using a drop-down value 我如何使用清单 <string> 在Linq中以实体作为实体? - How can I use List<string> in Linq to Entities as Entities? 在下拉列表中选择值时如何使用可折叠菜单 - How to use a collapsible menu when select value in a drop down list 如何在mvc5中填充下拉列表,以使每个列表都取决于上一个下拉列表? - How can i populate drop down lists in mvc5 such that each list is dependent on the previous drop down list? 如何使用Lambda / Linq过滤WCF服务结果 - How can I filter WCF service results using lambda / linq 如何根据另一个 LINQ 查询过滤结果? - How can I filter results of one LINQ query based on another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM