I need to filter data entity but there is no predefined columns, which I will have to filter.
public class EventDocument
{
public string ID1 { get; set; }
public int ID2 { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Number { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
I created all needed properties: SearchFirstName, SearchLastName, SearchNumber
And now I am trying to filter EventDocument by these parameters entered by user. If user enters value into SearchFirstName, my query will look like this:
var query = from b in DBContext.EventDocuments
where b.FirstName.Contains(SearchFirstName)
select b;
If user enters values to SearchFirstName and SearchLastName, my query will look like this:
var query = from b in DBContext.EventDocuments
where b.FirstName.Contains(SearchFirstName)
&& b.LastName.Contains(SearchLastName)
select b;
And how to construct query if I don't know - which filed user will fill? Maybe he will enter value to SearchLastName and SearchNumber...
You could do something like this
var query = DBContext.EventDocuments;
if(!string.IsNullOrEmpty(SearchFirstName))
{
query = query.Where(x => x.FirstName.Contains(SearchFirstName));
}
if(!string.IsNullOrEmpty(SearchLastName))
{
query = query.Where(x => x.LastName.Contains(SearchLastName));
}
var result = query.ToList();
and building your query based on what conditions you need.
Predicate Builder is an effective way.
Put following code in static PredicateBuilder class ,
public static Expression<Func<T, bool>> ContainsPredicate<T>(string memberName, string searchValue)
{
var parameter = Expression.Parameter(typeof(T), "m");
var member = Expression.PropertyOrField(parameter, memberName);
var body = Expression.Call(
member,
"Contains",
Type.EmptyTypes, // no generic type arguments
Expression.Constant(searchValue)
);
return Expression.Lambda<Func<T, bool>>(body, parameter);
}
In your cs file,
var filterResults = PredicateBuilder.ContainsPredicate<Employee>(columnName, searchName);
And yourLinqQuery.Where(filterResults);
For Reference: Predicate Builder Details
I would recommend you to construct the query dynamically using Dynamic Linq.
https://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library
var query = from eventDocument in DBContext.EventDocuments
where string.IsNullOrEmpty(firstName) || eventDocument.FirstName.Contains(firstName)
&& string.IsNullOrEmpty(lastName) || eventDocument.LastName.Contains(lastName)
&& string.IsNullOrEmpty(number) || eventDocument.Number.Contains(number)
select eventDocument;
Below worked for me
var query = from b in DBContext.EventDocuments
where (b.FirstName.Contains(SearchFirstName) && (!string.IsNullOrEmpty(SearchFirstName))
&& (b.LastName.Contains(SearchLastName) && (!string.IsNullOrEmpty(SearchLastName))
&& (b.SearchNumber.Contains(SearchNumber) && (!string.IsNullOrEmpty(SearchNumber))
select b;
I tried with below
List<EventDocument> lsteventDoc = new List<EventDocument>() {
new EventDocument{ID1 ="1", ID2=2, FirstName ="", LastName="Test", Number="1"},
new EventDocument{ID1 ="1", ID2=2, FirstName ="test", LastName="Test", Number="1"},
new EventDocument{ID1 ="1", ID2=2, FirstName ="", LastName="", Number="1"},
};
string SearchFirstName = "test";
string SearchLastName="Test";
string SearchNumber="1";
var query = from b in lsteventDoc
where (b.FirstName.Contains(SearchFirstName) && (!string.IsNullOrEmpty(SearchFirstName)))
&& (b.LastName.Contains(SearchLastName) && (!string.IsNullOrEmpty(SearchLastName)))
&& (b.Number.Contains(SearchNumber) && (!string.IsNullOrEmpty(SearchNumber)))
select b;
var query = from b in DBContext.EventDocuments
where (b.FirstName.Contains(SearchFirstName) || SearchFirstName == "")
&& (b.LastName.Contains(SearchLastName) || SearchLastName == "")
&& (b.Number.Contains(SearchNumber) || SearchNumber == "")
select b;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.