在linq查询中使用字典

在linq查询中使用字典,linq,dictionary,linq-to-entities,Linq,Dictionary,Linq To Entities,我想在我的linq查询中使用一些字典,但是因为linq到实体不能转换字典的使用,所以它会抛出一个异常。 实际上,以下问题中描述了相同的问题: 我对那里描述的解决方案不满意。我相信这个问题还有别的解决办法。我不想使用ToList/ToArray方法——它会将所有数据带到内存中 解决此问题的最佳方法是什么,而不将db数据拉入内存?您的示例看起来实际上不需要字典的功能,您只需要拥有WHERE IN功能。 您可以使用以下方法来实现此目的: var countries = Countries.Where

我想在我的linq查询中使用一些字典,但是因为linq到实体不能转换字典的使用,所以它会抛出一个异常。 实际上,以下问题中描述了相同的问题:

我对那里描述的解决方案不满意。我相信这个问题还有别的解决办法。我不想使用ToList/ToArray方法——它会将所有数据带到内存中


解决此问题的最佳方法是什么,而不将db数据拉入内存?

您的示例看起来实际上不需要字典的功能,您只需要拥有
WHERE IN
功能。
您可以使用以下方法来实现此目的:

var countries = Countries.WhereIn(x => x.CountryId, dict.Keys);
其中
不是内置的查询运算符,您需要自己编写或复制:

/// <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);
    }
}
//
///保存简化查询的扩展方法。
/// 
公共静态类QueryExtensions
{
/// 
///返回指定属性值包含在指定值中的元素。
/// 
///元素的类型。
///值的类型。
///消息来源。
///要测试的属性。
///财产的可接受价值。
///接受的元素。
公共静态iquiryable(
这是可靠的消息来源,
表达式属性选择器,
参数TValue[]值)
{
返回source.Where(GetWhereInExpression(propertySelector,values));
}
/// 
///返回指定属性值包含在指定值中的元素。
/// 
///元素的类型。
///值的类型。
///消息来源。
///要测试的属性。
///财产的可接受价值。
///接受的元素。
公共静态iquiryable(
这是可靠的消息来源,
表达式属性选择器,
IEnumerable值)
{
返回source.Where(GetWhereInExpression(propertySelector,values.ToList());
}
/// 
///获取“where in”条件的表达式。
/// 
///元素的类型。
///值的类型。
///属性选择器。
///价值观。
///表情。
私有静态表达式GetWhereInExpression(
表达式属性选择器,ICollection值)
{
var p=propertySelector.Parameters.Single();
如果(!values.Any())
返回e=>false;
var等于=
值。选择(
值=>
(表达式)Expression.Equal(propertySelector.Body,Expression.Constant(value,typeof(TValue)));
var body=等于.聚合(表达式.OrElse);
返回表达式.Lambda(body,p);
}
}

请显示您当前的-failing-code我可能错了,但我不相信您可以使用使用Linq to实体的字典而不首先将数据拉入内存。@DanielHilgarth这是一个示例:
Dictionary dict=new Dictionary();添加(1,1)条;dict.Add(2,2);var relevantEntities=(来自实体中的e,其中dict.ContainsKey(e.id)选择e)<如果您有时间,请在以下问题上帮助我:提前谢谢!