简体   繁体   English

LINQ搜索过滤器逻辑

[英]LINQ search filter logic

I have apartment entity and I want to get apartments based on my filters. 我有公寓实体,我想根据自己的过滤条件来获取公寓。

this is my search entity 这是我的搜索实体

public class Search
{
    public bool isStudio {get;set;}

    public bool isNoPlanning {get;set;}

    public bool isMultiRoom {get;set;}

    public int[] NumberOfRooms {get;set;}
}

this is my current search logic. 这是我当前的搜索逻辑。

var apartments = buildRepost.Get(buildId).Where(condStates => 
   (searchModel.NumberOfRooms != null 
     && searchModel.NumberOfRooms.Contains(condStates.RoomsCount.ToString()) 
   || ((searchModel.IsStudio && condStates.IsStudio)) 
   || ((searchModel.IsNoPlanning && condStates.IsFreePlaning)) 
   || ((searchModel.IsMultiRoom && condStates.RoomsCount >= 4)));

The problem with this logic is that I got the wrong result when all fields are false and null . 这种逻辑的问题是,当所有字段均为false和null时,我得到了错误的结果。 For example when isStudio , IsNoplaning and isMultiRoom are false and numberofRooms is null I should have got all apartments but instead, I got an empty array. 例如,当isStudioIsNoplaningisMultiRoom为false且numberofRoomsnull我应该拥有所有公寓,但是,我得到了一个空数组。 Any help? 有什么帮助吗?

the searchModel.NumberOfRooms != null checker in the where clause causes the problem, you are not mathcing it with any of the condStates properties and the searchModel.Is..... properties where子句中的searchModel.NumberOfRooms != null检查器会导致问题,您没有使用任何condStates属性和searchModel.Is.....属性来对其进行searchModel.Is.....

Make your searchModel as a checker in an if statement then build the query from the if searchModel conditions. 使您的searchModel成为if语句中的检查器,然后根据if searchModel条件构建查询。

var query = buildRepost.Get(buildId).AsQueryable();

if (searchModel.NumberOfRooms != null)
{
    query = query.Where(condStates  => searchModel.NumberOfRooms.Contains(condStates.RoomsCount.ToString());
}
if (searchModel.IsStudio)
{
    query = query.Where(condStates  => condStates.IsStudio); 
}
if (searchModel.IsNoPlaning)
{
    query = query.Where(condStates  => condStates.IsFreePlaning)
}
if (searchModel.IsMultiRoom)
{
    query = query.Where(condStates  => condStates.RoomsCount >= 4)
}

var results = query.ToList()
(searchModel.NumberOfRooms != null 
     && searchModel.NumberOfRooms.Contains(condStates.RoomsCount.ToString()) 

This is always going to be false when the NumberOfRooms is null (due to the null check), and given that the other bool values are all false, you will get no results. NumberOfRoomsnull (由于进行null检查),该值始终为false,并且鉴于其他bool值均为false,则不会获得任何结果。

Instead if you change to: 相反,如果您更改为:

(searchModel.NumberOfRooms == null 
     || searchModel.NumberOfRooms.Contains(condStates.RoomsCount.ToString()) 

You will either get everything (when the NumberOfRooms is null, or just the records that match the RoomsCount (when the NumberOfRooms is not null). 您将获得所有内容(当NumberOfRooms为空时,或者仅获取与RoomsCount匹配的RoomsCount (当NumberOfRooms不为空时)。

Note that in this case, if NumberOfRooms is null you will still return everything regardless of your bool filters. 请注意,在这种情况下,如果NumberOfRooms为null,则无论布尔过滤器如何,您仍将返回所有内容。 Which seems to be what your code requires, but I'm not sure is what you actually require, so you might want to check that. 这似乎是您的代码要求的,但是我不确定您的实际要求是什么,因此您可能需要检查一下。

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

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