在内存过滤器中的SubSonic .Filter() [英] SubSonic .Filter() in memory filter

查看:282
本文介绍了在内存过滤器中的SubSonic .Filter()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 > 异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。 

第36行:bool remove = false;
第37行:System.Reflection.PropertyInfo pi = o.GetType()。GetProperty(w.ColumnName);
第38行:if(pi.CanRead)
第39行:{
第40行:object val = pi.GetValue(o,null);

我正在调用像下面这样的调用方式吗?似乎没有关于使用这种方法的文档

  NavCollection objTopLevelCol = objNavigation.Where(Nav.Columns.NavHigherID,Comparison .Equals,0).Filter(); 

提前致谢

解决方案

您过滤的值需要是属性名称,而不是数据库列名称。 b
$ b

  lCol = objNavigation.Where(Nav.HigherIDColumn.PropertyName,Comparison.Equals,0).Filter(); 

或者这里有一个稍微更详细的方法,基于.Filter()方法。它似乎更好地工作(至少对我来说),通过显式创建预先在哪里:

$ sub $ S $ SubSonic.Where =新的SubSonic。哪里();
w.ColumnName = Nav.HigherIDColumn.PropertyName;
w.Comparison = SubSonic.Comparison.NotIn;
w.ParameterValue = new string [] {validvalue1,validvalue2};

lCol = objNavigation.Filter(w,false);

以下是覆写:

  ///< summary> 
///根据设置的标准过滤现有的集合。这是一个内存过滤器。
///保留所有现有的wheres。
///< / summary>
///<返回> NavCollection< / returns>
public NavCollection Filter(SubSonic.Where w)
{
return Filter(w,false);
}

///< summary>
///根据设置的标准过滤现有的集合。这是一个内存过滤器。
///如果不需要,现有的wheres可以被清除。
///< / summary>
///<返回> NavCollection< / returns>
public NavCollection Filter(SubSonic.Where,bool clearWheres)
{
if(clearWheres)
{
this.wheres.Clear();
}
this.wheres.Add(w);
return Filter();
}

///< summary>
///根据设置的标准过滤现有的集合。这是一个内存过滤器。
///感谢开发者的支持!
///< / summary>
///<返回> NavCollection< / returns>
public NavCollection Filter()
{
for(int i = this.Count - 1; i> -1; i--)
{
Nav o = this [i];
foreach(SubSonic.Where w this.wheres)
{
bool remove = false;
System.Reflection.PropertyInfo pi = o.GetType()。GetProperty(w.ColumnName);
if(pi!= null&& pi.CanRead)
{
object val = pi.GetValue(o,null);
if(w.ParameterValue is Array)
{
Array paramValues =(Array)w.ParameterValue;
foreach(paramValues中的对象arrayVal)
{
remove =!Utility.IsMatch(w.Comparison,val,arrayVal);
if(remove)
break;
}
}
else
{
remove =!Utility.IsMatch(w.Comparison,val,w.ParameterValue);



$ b if(remove)
{
this.Remove(o);
break;
}
}
}
返回这个;

$ / code>

而SubSonic 2.0实际上并不支持IsMatch函数的In / NotIn,所以这里是自定义的版本(在SubSonic \Utility.cs中):

$ p $ ,object objA,objB)
{
if(objA.GetType()!= objB.GetType())
return false;

bool isIntegerVal =(typeof(int)== objA.GetType());
bool isDateTimeVal =(typeof(DateTime)== objA.GetType());

switch(compare)
{
case SubSonic.Comparison.In:
case SubSonic.Comparison.Equals:
if(objA.GetType() == typeof(string))
返回IsMatch((string)objA,(string)objB);
else
return objA.Equals(objB);
大小写SubSonic.Comparison.NotIn:
大小写SubSonic.Comparison.NotEquals:
return!objA.Equals(objB);
case SubSonic.Comparison.Like:
return objA.ToString()。Contains(objB.ToString());
case SubSonic.Comparison.NotLike:
return!objA.ToString()。Contains(objB.ToString());
case SubSonic.Comparison.GreaterThan:
if(isIntegerVal)
{
return((int)objA>(int)objB);

else if(isDateTimeVal)
{
return((DateTime)objA>(DateTime)objB);
}
break;
case SubSonic.Comparison.GreaterOrEquals:
if(isIntegerVal)
{
return((int)objA> =(int)objB);
}
else if(isDateTimeVal)
{
return((DateTime)objA> =(DateTime)objB);
}
break;
case SubSonic.Comparison.LessThan:
if(isIntegerVal)
{
return((int)objA<(int)objB);

else if(isDateTimeVal)
{
return((DateTime)objA<(DateTime)objB);
}
break;
case SubSonic.Comparison.LessOrEquals:
if(isIntegerVal)
{
return((int)objA< =(int)objB);

else if(isDateTimeVal)
{
return((DateTime)objA< =(DateTime)objB);
}
break;
}
返回false;
}


i'm having some issues getting the .Filter() method to work in subsonic, and i'm constantly getting errors like the one below:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.    

Line 36:                     bool remove = false;
Line 37:                     System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
Line 38:                     if (pi.CanRead)
Line 39:                     {
Line 40:                         object val = pi.GetValue(o, null);

i'm making calls like the one below- is this the corrent way to use it? There seems to be no documentation on the use of this method

            NavCollection objTopLevelCol = objNavigation.Where(Nav.Columns.NavHigherID,Comparison.Equals, 0).Filter();

thanks in advance

解决方案

The value you filter against needs to be the Property Name, not the database column name.

You might try this:

lCol = objNavigation.Where(Nav.HigherIDColumn.PropertyName,Comparison.Equals, 0).Filter();

Or here's a slightly more verbose method that works for me based on custom overrides of the .Filter() method. It seemed to work better (for me at least) by explicitly creating the Where beforehand:

    SubSonic.Where w = new SubSonic.Where();
    w.ColumnName = Nav.HigherIDColumn.PropertyName;
    w.Comparison = SubSonic.Comparison.NotIn;
    w.ParameterValue = new string[] { "validvalue1", "validvalue2" };

    lCol = objNavigation.Filter(w, false);

Here's the overrides:

    /// <summary>
    /// Filters an existing collection based on the set criteria. This is an in-memory filter.
    /// All existing wheres are retained.
    /// </summary>
    /// <returns>NavCollection</returns>
    public NavCollection Filter(SubSonic.Where w)
    {
        return Filter(w, false);
    }

    /// <summary>
    /// Filters an existing collection based on the set criteria. This is an in-memory filter.
    /// Existing wheres can be cleared if not needed.
    /// </summary>
    /// <returns>NavCollection</returns>
    public NavCollection Filter(SubSonic.Where w, bool clearWheres)
    {
        if (clearWheres)
        {
            this.wheres.Clear();
        }
        this.wheres.Add(w);
        return Filter();
    }

    /// <summary>
    /// Filters an existing collection based on the set criteria. This is an in-memory filter.
    /// Thanks to developingchris for this!
    /// </summary>
    /// <returns>NavCollection</returns>
    public NavCollection Filter()
    {
        for (int i = this.Count - 1; i > -1; i--)
        {
            Nav o = this[i];
            foreach (SubSonic.Where w in this.wheres)
            {
                bool remove = false;
                System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                if (pi != null && pi.CanRead)
                {
                    object val = pi.GetValue(o, null);
                    if (w.ParameterValue is Array)
                    {
                        Array paramValues = (Array)w.ParameterValue;
                        foreach (object arrayVal in paramValues)
                        {
                            remove = !Utility.IsMatch(w.Comparison, val, arrayVal);
                            if (remove)
                                break;
                        }
                    }
                    else
                    {
                        remove = !Utility.IsMatch(w.Comparison, val, w.ParameterValue);
                    }
                }


                if (remove)
                {
                    this.Remove(o);
                    break;
                }
            }
        }
        return this;
    }

And SubSonic 2.0 doesn't actually support In/NotIn for the IsMatch function, so here's the customized version that does (in SubSonic\Utility.cs):

    public static bool IsMatch(SubSonic.Comparison compare, object objA, object objB)
    {
        if (objA.GetType() != objB.GetType())
            return false;

        bool isIntegerVal = (typeof(int) == objA.GetType());
        bool isDateTimeVal = (typeof(DateTime) == objA.GetType());

        switch (compare)
        {
            case SubSonic.Comparison.In:
            case SubSonic.Comparison.Equals:
                if (objA.GetType() == typeof(string))
                    return IsMatch((string)objA, (string)objB);
                else
                    return objA.Equals(objB);
            case SubSonic.Comparison.NotIn:
            case SubSonic.Comparison.NotEquals:
                return !objA.Equals(objB);
            case SubSonic.Comparison.Like:
                return objA.ToString().Contains(objB.ToString());
            case SubSonic.Comparison.NotLike:
                return !objA.ToString().Contains(objB.ToString());
            case SubSonic.Comparison.GreaterThan:
                if (isIntegerVal)
                {
                    return ((int)objA > (int)objB);
                }
                else if (isDateTimeVal)
                {
                    return ((DateTime)objA > (DateTime)objB);
                }
                break;
            case SubSonic.Comparison.GreaterOrEquals:
                if (isIntegerVal)
                {
                    return ((int)objA >= (int)objB);
                }
                else if (isDateTimeVal)
                {
                    return ((DateTime)objA >= (DateTime)objB);
                }
                break;
            case SubSonic.Comparison.LessThan:
                if (isIntegerVal)
                {
                    return ((int)objA < (int)objB);
                }
                else if (isDateTimeVal)
                {
                    return ((DateTime)objA < (DateTime)objB);
                }
                break;
            case SubSonic.Comparison.LessOrEquals:
                if (isIntegerVal)
                {
                    return ((int)objA <= (int)objB);
                }
                else if (isDateTimeVal)
                {
                    return ((DateTime)objA <= (DateTime)objB);
                }
                break;
        }
        return false;
    }

这篇关于在内存过滤器中的SubSonic .Filter()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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