如何将字符串变量传递给linq选择新的{}部分

时间:2014-03-06 12:59:08

标签: linq linq-to-sql asp.net-3.5

我只想使用linq创建搜索功能,其中包含多个存储到会话变量的ColumnNames。我正在使用一种方法:

public void FillGrid(string CommandName,string ColumnName, string SearchText)

它有三个存储会话值的字符串变量。

现在我只想用这个查询传递ColumnName:

var query1 = (from p in db.Posts
              join c in db.Categories on p.Category_id equals c.Id
              join u in db.Users on p.User_id equals u.Id
              where (p.ToUser_id == user_id || p.ToUser_id == null) && p.User_id != user_id
              orderby p.Sent_Datetime descending
              select new
              {
                  Id = p.Id,
                  Title = p.Title,
                  Publisher = u.First_name + " " + u.Last_name,
                  ToUser = p.ToUser_id,
                  PublishDate = p.Sent_Datetime,
                  IsFile = p.IsFileAttached,
                  CategoryName = c.Category_name,
                  status_name = (from s in db.Status where (s.Id == p.status_id) select s.status_name).FirstOrDefault(),
                  Group_name = (from g in db.Groups where (g.Id == p.group_id) select g.Group_name).FirstOrDefault(),
                  FileSize = p.TotalFileSize,
                  ColumnName = Sesssion["ColumnName"].ToString()
              }).Where(q => q.ColumnName.Contains(SearchText));

但是,ColumnName不提供任何文本,或者它可能不是此查询的一部分,我必须手动提供列名,因为。

我有多列,所以我不能使用这样的语句:

.Where(q => q.Tile.Contains(SearchText));

此查询适用于单列。但我有多列,所以我必须从外侧设置q.ColumnName。

1 个答案:

答案 0 :(得分:3)

我会为这类事情做一个扩展方法,为你的谓词构建一个表达式。

 public static class Helper
{
    public static IQueryable<T> FilterForColumn<T>(this IQueryable<T> queryable, string colName, string searchText)
    {
        if (colName != null && searchText != null)
        {
            var parameter = Expression.Parameter(typeof(T), "m");
            var propertyExpression = Expression.Property(parameter, colName);
            var searchExpression = Expression.Constant(searchText);
            var containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
            var body = Expression.Call(propertyExpression, containsMethod, searchExpression);
            var predicate = Expression.Lambda<Func<T, bool>>(body, new[] { parameter });
            return queryable.Where(predicate);
        }
        else
        {
            return queryable;
        }
    }
}

在您的案例中使用

var query1 = (from p in db.Posts
              join c in db.Categories on p.Category_id equals c.Id
              join u in db.Users on p.User_id equals u.Id
              where (p.ToUser_id == user_id || p.ToUser_id == null) && p.User_id != user_id
              orderby p.Sent_Datetime descending
              select new
              {
                  Id = p.Id,
                  Title = p.Title,
                  Publisher = u.First_name + " " + u.Last_name,
                  ToUser = p.ToUser_id,
                  PublishDate = p.Sent_Datetime,
                  IsFile = p.IsFileAttached,
                  CategoryName = c.Category_name,
                  status_name = (from s in db.Status where (s.Id == p.status_id) select s.status_name).FirstOrDefault(),
                  Group_name = (from g in db.Groups where (g.Id == p.group_id) select g.Group_name).FirstOrDefault(),
                  FileSize = p.TotalFileSize,
              }).FilterForColumn(Sesssion["ColumnName"].ToString(), SearchText);