简体   繁体   English

检查复杂对象上的字符串属性是否包含条件,如果不为null或为空

[英]Check if string properties on a complex object contain term if not null or empty

I am using a Search Class with 3 properties: Machine, Action, Part. 我正在使用具有3个属性的搜索类:机器,动作,零件。
The Search page has 3 textbox controls for user to search on these properties. “搜索”页面具有3个文本框控件,供用户搜索这些属性。 Is there an efficient way to search on any combination of these properties containing a value? 有没有一种有效的方法来搜索包含值的这些属性的任何组合?
So a user can potentially search on all 3 properties or any combination of the properties if they are not null or empty. 因此,用户可以搜索所有3个属性或属性的任意组合(如果它们不为null或为空)。

I am building logic to query database using EF based on the above. 我正在基于以上内容构建使用EF查询数据库的逻辑。

I do such stuff the following way (as an extension method): 我通过以下方式(作为扩展方法)进行此类操作:

public static IQueryable<MyClass> Filter(this IQueryable<MyClass> items, string machineQuery, string actionQuery, string partQuery)
{
    if (!String.IsNullOrWhiteSpace(machineQuery)
        items = items.Where(i => i.Machine.Contains(machineQuery);

    if (!String.IsNullOrWhiteSpace(actionQuery)
        items = items.Where(i => i.Action.Contains(actionQuery);

    if (!String.IsNullOrWhiteSpace(partQuery)
        items = items.Where(i => i.Part.Contains(partQuery);

    return items;
}

This is converted to a DB-side query by EF. EF将其转换为DB端查询。 You then use it like: 然后,您可以像这样使用它:

var filteredItems = dbContext.MyClasses.Filter(machineQuery, actionQuery, partQuery);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM