gpt4 book ai didi

linq - 在 linq 查询中使用字典

转载 作者:行者123 更新时间:2023-12-04 15:46:18 26 4
gpt4 key购买 nike

我想在我的 linq 查询中使用一些字典,但是由于 LINQ to entity 无法翻译字典的使用,它会引发异常。
实际上,在以下问题中描述了相同的问题:
linq to entity framework: use dictionary in query

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

在不将数据库数据拉到内存的情况下,解决此问题的最佳方法是什么?

最佳答案

你的例子看起来你实际上不需要字典的功能,你只是想要 WHERE IN功能。
您可以使用以下方法来实现这一点:

var countries = Countries.WhereIn(x => x.CountryId, dict.Keys);
WhereIn不是内置的查询运算符,您需要自己编写 - 或复制:
/// <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);
}
}

关于linq - 在 linq 查询中使用字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13513767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com