C#使用MongoDB資料庫的幫助類
阿新 • • 發佈:2019-02-01
if (orderByProperty != null)
{
query = isDescending ? query.OrderByDescending(orderByProperty) : query.OrderBy(orderByProperty);
}
else
{
// query = query.OrderBy(sortPropertyName, isDescending);
}
return query;
}
/// <summary>
/// 根據條件查詢資料庫,並返回物件集合
/// </summary>
/// <param name="match">條件表示式</param>
/// <param name="sortPropertyName">排序欄位</param>
/// <param name="isDescending">如果為true則為降序,否則為升序</param>
/// <returns></returns>
public IList<T> Find(String collName, Expression<Func<T, bool>> match, string sortPropertyName, bool isDescending = true)
{
return GetQueryable(collName, match, sortPropertyName, isDescending).ToList();
}
/// <summary>
/// 根據條件查詢資料庫,並返回物件集合
/// </summary>
/// <param name="query">條件表示式</param>
/// <param name="sortPropertyName">排序欄位</param>
/// <param name="isDescending">如果為true則為降序,否則為升序</param>
/// <returns></returns>
public IList<T> Find(String collName, FilterDefinition<T> query, string sortPropertyName, bool isDescending = true)
{
return GetQueryable(collName, query, sortPropertyName, isDescending).ToList();
}
/// <summary>
/// 根據條件查詢資料庫,並返回物件集合
/// </summary>
/// <param name="match">條件表示式</param>
/// <param name="orderByProperty">排序表示式</param>
/// <param name="isDescending">如果為true則為降序,否則為升序</param>
/// <returns></returns>
public IList<T> Find<TKey>(String collName, Expression<Func<T, bool>> match, Expression<Func<T, TKey>> orderByProperty, bool isDescending = true)
{
return GetQueryable<TKey>(collName, match, orderByProperty, isDescending).ToList();
}
/// <summary>
/// 根據條件查詢資料庫,如果存在返回第一個物件
/// </summary>
/// <param name="filter">條件表示式</param>
/// <returns>存在則返回指定的第一個物件,否則返回預設值</returns>
public T FindSingle(String collName, FilterDefinition<T> filter)
{
var coll = GetColletion(collName);
return coll.Find(filter).FirstOrDefault();
}
///// <summary>
///// 根據條件查詢資料庫,並返回物件集合(用於分頁資料顯示)
///// </summary>
///// <param name="query">條件表示式</param>
///// <param name="info">分頁實體</param>
///// <returns>指定物件的集合</returns>
//public IList<T> FindWithPager(string collName, FilterDefinition<T> query, PagerInfo info, string orderByProperty, bool isDescending = true)
//{
// int pageindex = (info.CurrenetPageIndex < 1) ? 1 : info.CurrenetPageIndex;
// int pageSize = (info.PageSize <= 0) ? 20 : info.PageSize;
// int excludedRows = (pageindex - 1) * pageSize;
// var find = GetQueryable(collName, query, orderByProperty, isDescending);
// info.RecordCount = (int)find.Count();
// return find.Skip(excludedRows).Limit(pageSize).ToList();
//}
/// <summary>
/// 根據條件查詢資料庫,並返回物件集合(用於分頁資料顯示)
/// </summary>
/// <param name="match">條件表示式</param>
/// <param name="info">分頁實體</param>
/// <returns>指定物件的集合</returns>
//public IList<T> FindWithPager(String collName, Expression<Func<T, bool>> match, PagerInfo info, string orderByProperty, bool isDescending = true)
//{
// int pageindex = (info.CurrenetPageIndex < 1) ? 1 : info.CurrenetPageIndex;
// int pageSize = (info.PageSize <= 0) ? 20 : info.PageSize;
{
query = isDescending ? query.OrderByDescending(orderByProperty) : query.OrderBy(orderByProperty);
}
else
{
// query = query.OrderBy(sortPropertyName, isDescending);
}
return query;
}
/// <summary>
/// 根據條件查詢資料庫,並返回物件集合
/// </summary>
/// <param name="match">條件表示式</param>
/// <param name="sortPropertyName">排序欄位</param>
/// <param name="isDescending">如果為true則為降序,否則為升序</param>
/// <returns></returns>
public IList<T> Find(String collName, Expression<Func<T, bool>> match, string sortPropertyName, bool isDescending = true)
{
return GetQueryable(collName, match, sortPropertyName, isDescending).ToList();
}
/// <summary>
/// 根據條件查詢資料庫,並返回物件集合
/// </summary>
/// <param name="query">條件表示式</param>
/// <param name="sortPropertyName">排序欄位</param>
/// <param name="isDescending">如果為true則為降序,否則為升序</param>
/// <returns></returns>
public IList<T> Find(String collName, FilterDefinition<T> query, string sortPropertyName, bool isDescending = true)
{
return GetQueryable(collName, query, sortPropertyName, isDescending).ToList();
}
/// <summary>
/// 根據條件查詢資料庫,並返回物件集合
/// </summary>
/// <param name="match">條件表示式</param>
/// <param name="orderByProperty">排序表示式</param>
/// <param name="isDescending">如果為true則為降序,否則為升序</param>
/// <returns></returns>
public IList<T> Find<TKey>(String collName, Expression<Func<T, bool>> match, Expression<Func<T, TKey>> orderByProperty, bool isDescending = true)
{
return GetQueryable<TKey>(collName, match, orderByProperty, isDescending).ToList();
}
/// <summary>
/// 根據條件查詢資料庫,如果存在返回第一個物件
/// </summary>
/// <param name="filter">條件表示式</param>
/// <returns>存在則返回指定的第一個物件,否則返回預設值</returns>
public T FindSingle(String collName, FilterDefinition<T> filter)
{
var coll = GetColletion(collName);
return coll.Find(filter).FirstOrDefault();
}
///// <summary>
///// 根據條件查詢資料庫,並返回物件集合(用於分頁資料顯示)
///// </summary>
///// <param name="query">條件表示式</param>
///// <param name="info">分頁實體</param>
///// <returns>指定物件的集合</returns>
//public IList<T> FindWithPager(string collName, FilterDefinition<T> query, PagerInfo info, string orderByProperty, bool isDescending = true)
//{
// int pageindex = (info.CurrenetPageIndex < 1) ? 1 : info.CurrenetPageIndex;
// int pageSize = (info.PageSize <= 0) ? 20 : info.PageSize;
// int excludedRows = (pageindex - 1) * pageSize;
// var find = GetQueryable(collName, query, orderByProperty, isDescending);
// info.RecordCount = (int)find.Count();
// return find.Skip(excludedRows).Limit(pageSize).ToList();
//}
/// <summary>
/// 根據條件查詢資料庫,並返回物件集合(用於分頁資料顯示)
/// </summary>
/// <param name="match">條件表示式</param>
/// <param name="info">分頁實體</param>
/// <returns>指定物件的集合</returns>
//public IList<T> FindWithPager(String collName, Expression<Func<T, bool>> match, PagerInfo info, string orderByProperty, bool isDescending = true)
//{
// int pageindex = (info.CurrenetPageIndex < 1) ? 1 : info.CurrenetPageIndex;
// int pageSize = (info.PageSize <= 0) ? 20 : info.PageSize;