如何在Entity框架中编写动态“like”查询?

时间:2012-10-27 19:19:09

标签: entity-framework entity-framework-4 linq-to-entities

这是我的代码:

//order my baselist is context.Entity 
public static GridData Getdata<T>(ObjectSet<T> baseList,
    int currentPage,
    int rowsPerPage,
    string sortcolumn,
    string sortord,
    string searchQuery,
    string searchColumns)where T: class{
var query = baseList.OrderBy("it." + sortcolumn + " " + sortord);
        string strPredicate = string.Empty;
        if (!string.IsNullOrEmpty(searchColumns))
        {
            strPredicate = "it." + searchColumns + " LIKE   @" + searchColumns + "  ";

            query = baseList.Where(strPredicate, new ObjectParameter(searchColumns, searchQuery)).OrderBy("it." + sortcolumn + " " + sortord);
        }
}

我的问题是我试图在实体框架中写下或形成一个类似的查询,似乎它不支持它。

2 个答案:

答案 0 :(得分:3)

您可以在实体框架中使用.Contains,即LIKE运算符等效项。

答案 1 :(得分:1)

你可以用这个

 query = baseList.Where(baseli=>baseli.Contains(searchColumns )).OrderBy("it." + sortcolumn + " " + sortord);

:)