Compiling a Linq to Entity Request from Several Parameters

I'm currently building a detailed search, and I'm trying to figure out how to compose a Linq query for my object.

I mainly have users who can select 1 * items in a list control. The part I can't wrap around is this:

how can I dynamically build Where AND (the field is equal to this field OR is equal to this OR ...) where the number of elements is an option.

Domain database Content selection fields: '26, 21, 22, 100, 164, 130 '

Example : (The idea is to be able to generate this depending on the number of elements selected)

Offre.Where(o=> o.Domain.Contains("26") || o.Domain.Contains("100") ) Offre.Where(o=> o.Domain.Contains("26") ) Offre.Where(o=> o.Domain.Contains("26") || o.Domain.Contains("100") || o.Domain.Contains("22") ) 

then I can easily get the received query as IQueryable and add to this object to build my query.

can someone point me in the right direction for my AND clause (OR .. OR)?

+1
linq entity-framework
source share
3 answers

To get around this limitation, you can manually create an expression (Source)

FROM#

 static Expression<Func<TElement, bool>> BuildContainsExpression<TElement, TValue>( Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values) { if (null == valueSelector) { throw new ArgumentNullException("valueSelector"); } if (null == values) { throw new ArgumentNullException("values"); } ParameterExpression p = valueSelector.Parameters.Single(); // p => valueSelector(p) == values[0] || valueSelector(p) == ... if (!values.Any()) { return e => false; } var equals = values.Select(value => (Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue)))); var body = equals.Aggregate<Expression>((accumulate, equal) => Expression.Or(accumulate, equal)); return Expression.Lambda<Func<TElement, bool>>(body, p); } 

Using this utility method,

 var query2 = context.Entities.Where(BuildContainsExpression<Entity, int>(e => e.ID, ids)); 

Vb.net

 Public Shared Function BuildContainsExpression(Of TElement, TValue)( _ ByVal valueSelector As Expression(Of Func(Of TElement, TValue)), _ ByVal values As IEnumerable(Of TValue) _ ) As Expression(Of Func(Of TElement, Boolean)) ' validate arguments If IsNothing(valueSelector) Then Throw New ArgumentNullException("valueSelector") If IsNothing(values) Then Throw New ArgumentNullException("values") Dim p As ParameterExpression = valueSelector.Parameters.Single() If Not values.Any Then Return _ Function(e) False End If Dim equals = values.Select( _ Function(v) _ Expression.Equal(valueSelector.Body, Expression.Constant(v, GetType(TValue))) _ ) Dim body = equals.Aggregate( _ Function(accumulate, equal) _ Expression.Or(accumulate, equal) _ ) Return Expression.Lambda(Of Func(Of TElement, Boolean))(body, p) End Function 

Using this utility method

  Dim query = m_data. Offer If (selectedSectors.Count > 0) Then query = query.Where(BuildContainsExpression(Function(o As Offer) o.Value, selectedSectors)) End If 
+2
source share

I think this is your answer:

LINQ for Entities - Creating Suggestions for Testing Collections Between Many, Many

+2
source share

If your Office has a unique identifier associated with it, you can try the following:

 List<int> queryCriteria = new List<int>; //Fill your query criteria here //Instead of o.Domain.Id you can use whatever ID you have. var resultSet = Offre.Where(o => queryCriteria.Contains(o.Domain.Id)); 
0
source share

All Articles