通用助手获取将匹配任何给定EntityObject的ObjectResult <t> </t>

时间:2013-08-30 01:22:48

标签: c# asp.net entity-framework extension-methods helpermethods

试图通过一种方法来创建一个通用的SQL查询,该方法将获取我正在制作此代码的数据

public ObjectResult<theSelectedTableNameModel> getUsersRes(string sqlStr, string ColNameAsfilter, string FiltersValue,string OrderByFilter="")
{
    string SqlCmd = "";
    string By = "";
    if (OrderByFilter.isNotEmptyOrNull())
       By = string.Concat(" ORDER BY ", OrderByFilter);
    SqlCmd = string.Format("{0} WHERE {1}={2}{3}", SqlStr, ColNameAsfilter, FiltersValue, By);


    return anEntityName.ExecuteStoreQuery<theSelectedTableNameModel>(SqlCmd);
}

我复制了我的代码并编辑了真实姓名和其他变量/参数,所以我可能犯了一个错误,但问题是,我怎么能让它更通用呢?

这是一种工作方法,可以让我指定sqlCommand

的查询

我希望它适合任何实体和任何模型/对象/表

怎么可能呢? 我想有一个现成的解决方案,或者EF的创造不是通用的...... 我正在使用asp.net 4.0和最新的EF ..

3 个答案:

答案 0 :(得分:0)

我认为你需要这样的东西

    public List<T> Get(System.Linq.Expressions.Expression<Func<T, bool>> filter = null)
    {
        IQueryable<T> query = dbSet.AsQueryable();
        if (filter != null)
        {
            query = query.Where(filter);
        }
        return query.ToList();
    }

关注此link

答案 1 :(得分:0)

为什么不考虑将sql参数的集合作为参数传递给函数调用。执行此操作后,您可以使用任意数量的多种类型的过滤器

public ObjectResult<theSelectedTableNameModel> GetEntityBySQLCommand(string sqlStr, SqlParameter[] filterParams, string OrderByFilter="")
{
    var commandTextToExecute = OrderByFilter.isNotEmptyOrNull() ? sqlStr : string.Format(“{0} Order By {1}”, sqlStr, OrderByFilter);

    return yourObjectContext.ExecuteStoreQuery<theSelectedTableNameModel>(commandTextToExecute, filterParams);
}

您的sqlStr应该类似于以下“SELECT * FROM Customer WHERE CustId = @custID and LastActiveOn = @lastActiveDate”; custID lastActiveDate 应该通过SqlParameter集合传递(filterParams)

答案 2 :(得分:0)

经过一段时间测试每个可能的选项,而我在网上没有找到任何类似的代码..

这是我的解决方案,作为一种扩展方法,我是否进行了糟糕的搜索/研究?或者只是没有人像这样使用它,虽然extesion是针对类型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);

}

任何类型的评论都会有所帮助(好吧......几乎任何评论)