Question

Ii just want to make search functionality with linq with multiple ColumnNames that stored to session variable. I'm using one method:

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

That has three string variable that stores session value.

Now I just want to pass ColumnName with this query:

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));

However, ColumnName does not give any text or it may be not part of this query i have to manually give column name because.

for multiple column i have, so i can not use this statement like:

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

this query works fine with single column. but there is multiple column i have so i have to set q.ColumnName from outer side.

Was it helpful?

Solution

I would do an extension method for that kind of things, building an expression for your predicate.

 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;
        }
    }
}

usage in your case

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top