3 回答
TA贡献1851条经验 获得超4个赞
我是很新,Linq和EF
您很不走运,因为“简单的事情”在LINQ to Objects中相对容易,但是在LINQ to Entities中却相当困难(几乎不可能)。
为了以某种方式解决它,您需要手动构建LINQ to Entitiescompatible Expression。
首先,您需要一些帮助程序来构建表达谓词。PredicateBuilder是一个流行的选择,但它不会产生EF兼容表情和要求LinqKit,并AsExpandable在仓库里。因此,我使用以下类似的辅助方法,但会产生最终的兼容表达式:
public static class PredicateUtils
{
sealed class Predicate<T>
{
public static readonly Expression<Func<T, bool>> True = item => true;
public static readonly Expression<Func<T, bool>> False = item => false;
}
public static Expression<Func<T, bool>> Null<T>() { return null; }
public static Expression<Func<T, bool>> True<T>() { return Predicate<T>.True; }
public static Expression<Func<T, bool>> False<T>() { return Predicate<T>.False; }
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
{
if (Equals(left, right)) return left;
if (left == null || Equals(left, True<T>())) return right;
if (right == null || Equals(right, True<T>())) return left;
if (Equals(left, False<T>()) || Equals(right, False<T>())) return False<T>();
var body = Expression.AndAlso(left.Body, right.Body.Replace(right.Parameters[0], left.Parameters[0]));
return Expression.Lambda<Func<T, bool>>(body, left.Parameters);
}
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
{
if (Equals(left, right)) return left;
if (left == null || Equals(left, False<T>())) return right;
if (right == null || Equals(right, False<T>())) return left;
if (Equals(left, True<T>()) || Equals(right, True<T>())) return True<T>();
var body = Expression.OrElse(left.Body, right.Body.Replace(right.Parameters[0], left.Parameters[0]));
return Expression.Lambda<Func<T, bool>>(body, left.Parameters);
}
static Expression Replace(this Expression expression, Expression source, Expression target)
{
return new ExpressionReplacer { Source = source, Target = target }.Visit(expression);
}
class ExpressionReplacer : ExpressionVisitor
{
public Expression Source;
public Expression Target;
public override Expression Visit(Expression node)
{
return node == Source ? Target : base.Visit(node);
}
}
}
其次,在控制器中为这样的单个条件定义一个辅助方法
static Expression<Func<Person, bool>> AbilityFilter(int index, int value)
{
return p => p.AllAbilities.OrderBy(a => a.Id).Skip(index).Take(1).Any(a => a.Value > value);
}
最后,构建过滤器并将其传递给GetAll方法:
var filter = PredicateUtils.Null<Person>();
for (int i = 0; i < AbilitiesInput.Count; i++)
filter = filter.And(AbilityFilter(i, AbilitiesInput[i]));
GetAll(filter);
使用的技术绝对不是新手,但我认为没有简单的方法可以解决该特定问题。
TA贡献1801条经验 获得超8个赞
不知道我是否正确,但是下面的代码可能有助于获得正确的解决方案。使用(x,i)将枚举整个集合并获得一个索引,以便您可以比较两个集合。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
List<TextBox> AbilitiesInput = null;
public Form1()
{
InitializeComponent();
AbilitiesInput = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4 };
Person person = new Person();
List<Ability> results = person.AllAbilities.Where((x, i) => x.Value > int.Parse(AbilitiesInput[i].Text)).ToList();
}
}
public class Person
{
public int Id { get; set; }
public List<Ability> AllAbilities { get; set; }
public Person()
{
AllAbilities = new List<Ability>();
}
}
public class Ability
{
public int Id { get; set;}
public int PersonId { get; set; }
public int Value { get; set; }
}
}
TA贡献1801条经验 获得超16个赞
非常感谢您的答复,但是为了在查询的where子句中使用它,我需要一个布尔值而不是List<Ability> results
。我如何将其更改为布尔值?我尝试用替换Where
,All
但All
似乎无法处理(x, i)
。
- 3 回答
- 0 关注
- 429 浏览
添加回答
举报