经过一段时间的测试每个可能的选项,而我没有在网上找到任何类似的代码..
这是我的解决方案,作为一种扩展方法,我是否进行了错误的搜索/研究?或者只是没有人像这样使用它,尽管扩展是针对ObjectContext
它确实返回一个ObjectResult
可选参数列表的类型,用于 where 子句 + 另一个用于 order by 并且它完成了工作
string firstPartSql="SELECT * FROM YourTblName";
WHERE 过滤器的字符串列表将包含以下元素
"columnName=value", "columnName Like '%val%'"
Order by 的字符串列表将包含以下元素
"ColumnName" ,"ColumnName DESC"
public static ObjectResult<T> getUsersResStatic<T>(this ObjectContext Entt, string sqlBeginhing, List<string> LstSelectWhersFilter = null, List<string> LstOby = null)
{
string SqlCmd = "";
string StrWhereFilterPrefix = "";
string StrFinalWHEREFiter ="";
string StrKeyOb1 = "";
string StrKeyOb2 = "";
string StrFinalOrderBy = "";
if (LstSelectWhersFilter != null)
{
StrWhereFilterPrefix = "WHERE";
for (int CountWhers = 0; CountWhers < LstSelectWhersFilter.Count; CountWhers++)
{
if (CountWhers == 0) StrFinalWHEREFiter = string.Format(" {0} {1} ", StrWhereFilterPrefix, LstSelectWhersFilter.ElementAt(CountWhers));
else
{
StrWhereFilterPrefix = "AND";
StrFinalWHEREFiter += string.Format(" {0} {1} ", StrWhereFilterPrefix, LstSelectWhersFilter.ElementAt(CountWhers));
}
}
}
if (LstOby != null)
{
StrKeyOb1 = "ORDER BY";
if (LstOby.Count > 1)
{
StrKeyOb2 = ",";
for (int i = 0; i < LstOby.Count; i++)
{
if (i == 0) StrFinalOrderBy = string.Format("{0} {1}", StrKeyOb1, LstOby.ElementAt(i));
else
StrFinalOrderBy += string.Format("{0} {1}", StrKeyOb2, LstOby.ElementAt(i));
}
}
else StrFinalOrderBy = string.Format("{0} {1}", StrKeyOb1, LstOby.ElementAt(0));
SqlCmd = string.Format("{0} {1} {2}", sqlBeginhing, StrFinalWHEREFiter, StrFinalOrderBy);//StrKeyOb2, ob2);
}
if (LstSelectWhersFilter == null && LstOby == null) SqlCmd = sqlBeginhing;
return Entt.ExecuteStoreQuery<T>(SqlCmd);
}
任何类型的评论都会有所帮助(嗯......几乎任何评论)