如何忽略搜索条件中的空属性 [英] How to ignore null properties in the search criteria

查看:58
本文介绍了如何忽略搜索条件中的空属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不好的要求要做;无论如何,我必须在我的应用程序中实现它.

I have got a bad requirement to do; anyway I have to implement that in my application.

我有一个Track

public class Track
{
    public string Name { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

我有一些测试数据

List<Track> Records = new List<Track>
{ 
    new Track { City = "a", Name = "a",  Country = "i" }, // Track 1
    new Track { City = "b", Name = "b",  Country = "i" }, // Track 2
    new Track { City = "a", Name = null, Country = "J" }, // Track 3
    new Track { City = "c", Name = "a",  Country = "J" }, // Track 4
    new Track { City = "b", Name = "a",  Country = null}, // Track 5
};

要求是我应该基于传递的值从Records查询数据.如果任何属性为null,则搜索条件应忽略该属性.它应该基于NonNull属性进行查询.

Requirement is i should query the data from Records based on values passed. If any of the property is null then search criteria should ignore that property. It should query based on NonNull properties.

示例:

If i query for City = a, Name = "b", Country = "i" then Result is: Track 1 & Track 3
If i query for City = c, Name = "p", Country = "w" then Result is: Track 4

Name& CountryTrack 3&中具有空值. Track 5.因此它将在搜索中忽略.希望很清楚

Name & Country have null values in Track 3 & Track 5.So it will ignore in search. Hope it is clear

我终于有了下面的逻辑

var filterRecords = new List<Track>();
if (!Records.Any(t => string.IsNullOrWhiteSpace(t.City)))
{
    filterRecords = Records.Where(c => c.City == _city).ToList();  // Here _city is the method parameter.. assume "a"
}

if (!Records.Any(t => string.IsNullOrWhiteSpace(t.Country)))
{
    filterRecords = filterRecords.Where(c => c.City == _country).ToList();  // Here _country is the method parameter.. assume "c"
}

Track类具有12个属性.像上面那样检查12次不是一个好兆头. 我想通过使用LINQ或其他任何简单的方法来实现这一点.

Track class has 12 properties. Checking for 12 times like above is not good sign. I would like to achieve this by using LINQ or any other which is simple.

有什么建议吗?

推荐答案

我想到的最佳解决方案是构建聚合过滤器(您可以为此使用Track对象,因为它已经具有用于过滤集合的所有可能的属性,并且它们可以为空):

Best solution came to my mind is to build aggregate filter (You can use your Track object for that, because it already has all possible properties for filtering collection, and they are nullable):

Track filter = records.Aggregate(
    new Track() { City = _city, Country = _country, Name = _name },
    (f, t) => new Track()
    {
        City = String.IsNullOrEmpty(t.City) ? null : f.City, 
        Country = String.IsNullOrEmpty(t.Country) ? null : f.Country,
        Name = String.IsNullOrEmpty(t.Name) ? null : f.Name
    },
    f => f);

这仅需要对集合进行一次迭代即可定义哪些字段为空.然后只需将过滤器应用于您的记录:

This will require only one iteration over collection to define which fields has null. Then simply apply filter to your records:

var query = from t in Records
            where (filter.City == null || t.City == filter.City) &&
                    (filter.Country == null || t.Country == filter.Country) &&
                    (filter.Name == null || t.Name == filter.Name)
            select t;

这篇关于如何忽略搜索条件中的空属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆