Hi Steven,
Thanks for your post. My name is Hongye Sun [MSFT] and it is my pleasure
to
work with you on this issue.
We have successfully reproduce the issue in our lab. After performing
research on this issue, we found that Entity Framework does not currently
support collection-valued parameters. To bypass this restriction, we
worked
out one workaround for you.
Building a containing expression
First, please copy the following utility code into your project.
---------------------------------
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();
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);
}
---------------------------------
Then, change your original code:
var Items = from items in db.DBItems
where ChildrenIDs.Contains(items.CategoryID)
select items;
Into the following code:
---------------------------------
var Items = db.Items.Where((BuildContainsExpression<Item, int>(item =>
item.CategoryID, ChildrenIDs));
---------------------------------
For your convenience, we also translate the utility code into VB.net. Hope
it helps.
---------------------------------
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
---------------------------------
We have successfully fixed this issue by using this workaround in our lab.
Please have a try on your side and let us know if it works for you.
Thanks.
Regards,
Hongye Sun (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support
Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.