gpt4 book ai didi

c# - '参数 'f' 未绑定(bind)到指定的 LINQ to Entities 查询表达式'

转载 作者:行者123 更新时间:2023-11-30 12:59:09 25 4
gpt4 key购买 nike

使用 EF 6。有人可以告诉我我做错了什么吗?

public List<SearchResult> SearchDocuments(List<SearchCriteria> searchCriterias)
{
List<SearchResult> results = new List<SearchResult>();
var fieldSettings = LoadCoordinateSystemFieldSettings(searchCriterias);
using (var context = CreateContext())
{
var tractsQuery = PredicateBuilder.False<v_UploadByTract_ActiveUploads>();
foreach (var searchCriteria in searchCriterias)
{
var queryBuilder = PredicateBuilder.True<v_UploadByTract_ActiveUploads>();

// tractsQuery
if (searchCriteria.StateAPI.HasValue)
queryBuilder = queryBuilder.And(a => a.StateAPI == searchCriteria.StateAPI.Value);
if (searchCriteria.CountyAPI.HasValue)
queryBuilder = queryBuilder.And(a => a.CountyAPI == searchCriteria.CountyAPI.Value);

// ...
// ... many more similar IF-ANDs
// ...

tractsQuery = tractsQuery.Or(queryBuilder);
}

var searchQuery = context.v_UploadByTract_ActiveUploads.AsExpandable().Where(tractsQuery).ToList();
//.Select(a => SearchResult.Create(a));
//results.AddRange(searchQuery.ToList());
}

return results;
}

public static class PredicateBuilder
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }

public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters);
}

public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.AndAlso(expr1.Body, invokedExpr), expr1.Parameters);
}
}

最佳答案

quetzalcoatl 感谢您的回答。另一个帖子做到了。从其他地方我遇到了 AsExpandable() 并忽略了这个认为它在谈论相同的东西。阅读您的帖子并重新阅读讨论后,这很有效。由于您的帖子显示为“评论”,因此我无法将其标记为答案。关于如何巩固那些讨厌的 IF 的任何建议。

C# PredicateBuilder Entities: The parameter 'f' was not bound in the specified LINQ to Entities query expression

关于c# - '参数 'f' 未绑定(bind)到指定的 LINQ to Entities 查询表达式',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26908853/

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