Your example looks like you actually don't need the functionality of a dictionary, you simply want to have WHERE IN
functionality.
You could use the following to achieve this:
var countries = Countries.WhereIn(x => x.CountryId, dict.Keys);
WhereIn
is not a built in query operator, you need to write it yourself - or copy:
/// <summary>
/// Holds extension methods that simplify querying.
/// </summary>
public static class QueryExtensions
{
/// <summary>
/// Return the element that the specified property's value is contained in the specified values.
/// </summary>
/// <typeparam name="TElement"> The type of the element. </typeparam>
/// <typeparam name="TValue"> The type of the values. </typeparam>
/// <param name="source"> The source. </param>
/// <param name="propertySelector"> The property to be tested. </param>
/// <param name="values"> The accepted values of the property. </param>
/// <returns> The accepted elements. </returns>
public static IQueryable<TElement> WhereIn<TElement, TValue>(
this IQueryable<TElement> source,
Expression<Func<TElement, TValue>> propertySelector,
params TValue[] values)
{
return source.Where(GetWhereInExpression(propertySelector, values));
}
/// <summary>
/// Return the element that the specified property's value is contained in the specified values.
/// </summary>
/// <typeparam name="TElement"> The type of the element. </typeparam>
/// <typeparam name="TValue"> The type of the values. </typeparam>
/// <param name="source"> The source. </param>
/// <param name="propertySelector"> The property to be tested. </param>
/// <param name="values"> The accepted values of the property. </param>
/// <returns> The accepted elements. </returns>
public static IQueryable<TElement> WhereIn<TElement, TValue>(
this IQueryable<TElement> source,
Expression<Func<TElement, TValue>> propertySelector,
IEnumerable<TValue> values)
{
return source.Where(GetWhereInExpression(propertySelector, values.ToList()));
}
/// <summary>
/// Gets the expression for a "where in" condition.
/// </summary>
/// <typeparam name="TElement"> The type of the element. </typeparam>
/// <typeparam name="TValue"> The type of the value. </typeparam>
/// <param name="propertySelector"> The property selector. </param>
/// <param name="values"> The values. </param>
/// <returns> The expression. </returns>
private static Expression<Func<TElement, bool>> GetWhereInExpression<TElement, TValue>(
Expression<Func<TElement, TValue>> propertySelector, ICollection<TValue> values)
{
var p = propertySelector.Parameters.Single();
if (!values.Any())
return e => false;
var equals =
values.Select(
value =>
(Expression)Expression.Equal(propertySelector.Body, Expression.Constant(value, typeof(TValue))));
var body = equals.Aggregate(Expression.OrElse);
return Expression.Lambda<Func<TElement, bool>>(body, p);
}
}
Dictionary dict = new Dictionary();
dict.Add(1,1);
dict.Add(2,2);
var relevantEntities = (from e in Entities
where dict.ContainsKey(e.id)
select e);
– Tomer Peled Nov 22 '12 at 14:35