处置过滤器数据时如何在领域中使用I过滤器

时间:2019-02-15 11:03:46

标签: .net xamarin realm

我已经尝试过像这样在领域集合中过滤IQueryable <>:

public class Configuration
{
    public bool IsFilter { get; set; }

    public IQueryable<Model> ApplyFilter(IQueryable<Model> collection)
    {
        var filterCollection = collection;

        filterMessages = filterMessages.Where(IsFilter || !IsFilter && w.IsFilter);

        return filterCollection;
    }
}

当我使用ApplyFilter时,我会得到NotSupportedException

我认为这是因为Configuration的对象已经被处置和收集了。

如何使用类似的过滤器?

2 个答案:

答案 0 :(得分:0)

如果您只是想通过包含的bool属性过滤Realm模型,则可以使用Linq Where({Where(model => model.IsFilter == IsFilter)

示例:

public class Configuration
{
    public bool IsFilter { get; set; }

    public IQueryable<ItemModel> ApplyFilter(IQueryable<ItemModel> realmQuery)
    {
        return realmQuery.Where(model => model.IsFilter == IsFilter);
    }
}

示例用法:

// Assuming you have an `Configuration` instance already created:
configuration.IsFilter = true;
realmList.ItemsSource = configuration.ApplyFilter(realmList.ItemsSource as IQueryable<ItemModel>);

答案 1 :(得分:-1)

请注意,当前,该属性必须位于条件的左侧。这意味着

var oldDogs = realm.All<Dog>().Where(dog => 7 < dog.Age); // INVALID query, do not copy
is illegal and would have to be changed into the equivalent

var oldDogs = realm.All<Dog>().Where(dog => dog.Age > 7); // Fixed

https://realm.io/docs/dotnet/latest/api/linqsupport.html

感谢@SushiHangover

但是我认为“领域”应该提供描述信息。