真的很尷尬的問題 - 我有亞音速收集,然後我用Where過濾掉一些數據。如何將IEnumerable轉換爲Subsonic集合?
MyColl.Where(it => it.foo()==true)
現在我想將這些數據仍然作爲Subsonic集合。如何做到這一點(最合適的方式)?我檢查了Subsonic集合,ToX()方法的構造函數,並使用Google搜索。
編輯:Subsonic 2.x系列。
在此先感謝您,對於天真的問題抱歉。
真的很尷尬的問題 - 我有亞音速收集,然後我用Where過濾掉一些數據。如何將IEnumerable轉換爲Subsonic集合?
MyColl.Where(it => it.foo()==true)
現在我想將這些數據仍然作爲Subsonic集合。如何做到這一點(最合適的方式)?我檢查了Subsonic集合,ToX()方法的構造函數,並使用Google搜索。
編輯:Subsonic 2.x系列。
在此先感謝您,對於天真的問題抱歉。
在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;
}
}
出於某種原因,我永遠無法得到過濾器的直線排列方式工作,但其易於使用這樣的:
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 :-) – greenoldman 2010-01-14 07:26:08
謝謝你爲指出版本,我忘了提及它是2.x系列。我沒有看到過濾方法。 – greenoldman 2010-01-05 07:59:46
@macias - 抱歉,延遲迴復!我添加了一個代碼示例。 – 2010-01-13 23:16:16