【问题标题】:How to convert IEnumerable to Subsonic collection?如何将 IEnumerable 转换为 Subsonic 集合?
【发布时间】:2009-12-30 12:04:49
【问题描述】:

真是令人尴尬的问题——我有 Subsonic 集合,然后我使用 Where 过滤掉一些数据。

MyColl.Where(it => it.foo()==true)

现在我想将这些数据仍然作为 Subsonic 集合传递。怎么做(最正确的方法)?我检查了 Subsonic 集合、ToX() 方法的构造函数,并用谷歌搜索。

编辑:Subsonic 2.x 系列。

提前感谢您,对于幼稚的问题,我们深表歉意。

【问题讨论】:

    标签: linq subsonic ienumerable collections


    【解决方案1】:

    在 SubSonic 2.x 中,数据实际上并没有被 Where() 过滤掉,直到对数据库重新执行查询。我假设这在 3.0 中是相同的。在 2.x 中有一个 .Filter() 方法可以满足您的需求。

    .Filter() 方法被添加到生成的类中。这是我的样子(它是默认定制的):

        /// <summary>
        /// Filters an existing collection based on the set criteria. This is an in-memory filter.
        /// All existing wheres are retained.
        /// </summary>
        /// <returns>TblSomethingOrOtherCollection</returns>
        public TblSomethingOrOtherCollection 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>TblSomethingOrOtherCollection</returns>
        public TblSomethingOrOtherCollection 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>TblSomethingOrOtherCollection</returns>
        public TblSomethingOrOtherCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                TblSomethingOrOther 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;
        }
    
    
    }
    

    【讨论】:

    • 感谢指出版本,我忘了说它是2.x系列。我没有看到 Filter 方法。
    • @macias - 抱歉延迟回复!我添加了一个代码示例。
    【解决方案2】:

    由于某种原因,我永远无法让过滤器的内联方法工作,但它易于使用,如下所示:

     SubSonic.Where w = new Where();
            w.ColumnName = Product.CatIDColumn.PropertyName;
            w.Comparison = Comparison.Equals;
            w.ParameterValue = "1";
    
     ProductCollection objFilteredCol = objProdCollection.Where(w).Filter();
    

    【讨论】:

    • 谢谢您,从您的示例(您和 Ranomore 的示例)中可以清楚地看出,我至少需要对 Subsonic 进行轻微升级 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多