C# 如何选择表达式的评估值<;Func<;T>&燃气轮机;实体框架内查询
我正在创建一个在EF中生成变量搜索查询的系统。为了实现这一点,我动态地构建了一个C# 如何选择表达式的评估值<;Func<;T>&燃气轮机;实体框架内查询,c#,.net,entity-framework,expression,C#,.net,Entity Framework,Expression,我正在创建一个在EF中生成变量搜索查询的系统。为了实现这一点,我动态地构建了一个表达式,如下所示 Expression<Func<Entity, float>> matchScore = x => 0; if (!string.IsNullOrWhiteSpace(NameLTB.Value)) { var words = NameLTB.Value.Split(' ').ToArray(); var perWordValue = 2f / word
表达式
,如下所示
Expression<Func<Entity, float>> matchScore = x => 0;
if (!string.IsNullOrWhiteSpace(NameLTB.Value))
{
var words = NameLTB.Value.Split(' ').ToArray();
var perWordValue = 2f / words.Length;
foreach (var word in words)
matchScore = ExpressionExt.Compose(matchScore, x => x.Name.Contains(word) ? perWordValue : 0f, Expression.Add);
}
//...
List<Entity> result;
using (var context = new DatabaseContext())
result = context.Table.OrderByDescending(matchScore).Take(MaxResults).ToList();
表达式匹配分数=x=>0;
如果(!string.IsNullOrWhiteSpace(NameLTB.Value))
{
var words=NameLTB.Value.Split(“”).ToArray();
var perWordValue=2f/words.Length;
foreach(单词中的var单词)
matchScore=ExpressionExt.Compose(matchScore,x=>x.Name.Contains(word)?perWordValue:0f,Expression.Add);
}
//...
然后我对这个EF兼容的表达式
进行排序,如下所示
Expression<Func<Entity, float>> matchScore = x => 0;
if (!string.IsNullOrWhiteSpace(NameLTB.Value))
{
var words = NameLTB.Value.Split(' ').ToArray();
var perWordValue = 2f / words.Length;
foreach (var word in words)
matchScore = ExpressionExt.Compose(matchScore, x => x.Name.Contains(word) ? perWordValue : 0f, Expression.Add);
}
//...
List<Entity> result;
using (var context = new DatabaseContext())
result = context.Table.OrderByDescending(matchScore).Take(MaxResults).ToList();
列表结果;
使用(var context=new DatabaseContext())
结果=context.Table.OrderByDescending(matchScore).Take(MaxResults.ToList();
这里的问题是,我不知道是什么分数导致了订单变成了现在的样子。我希望能做到这一点
List<Entity> result;
using (var context = new DatabaseContext())
result =
(
from x in context.Table
let y = matchScore
orderby y descending
select new { Entity = x, MatchScore = y }
)
.ToList();
列表结果;
使用(var context=new DatabaseContext())
结果=
(
从context.Table中的x开始
让y=匹配分数
按顺序递减
选择新{Entity=x,MatchScore=y}
)
.ToList();
当然这里的问题是
y是无效的表达式。我可以使用。选择(matchScore)
,但没有原始实体。您可以将表达式与以下内容组合:
Expression<Func<Entity, float>> matchScore = x => 0;
Expression<Func<Entity, float>> matchScoreInc;
string s = "a b";
if (!string.IsNullOrWhiteSpace(s))
{
var words = s.Split(' ').ToArray();
var perWordValue = 2f / words.Length;
foreach (var word in words)
{
matchScoreInc = x => matchScore.Invoke(x) + (x.Name.Contains(word) ? perWordValue : 0f);
matchScore = matchScoreInc.Expand();
}
}
var q =
from x in table.AsExpandable()
let y = matchScore.Invoke(x)
orderby y descending
select new { Entity = x, MatchScore = y };
表达式匹配分数=x=>0;
表达公司;
字符串s=“a b”;
如果(!string.IsNullOrWhiteSpace))
{
var words=s.Split(“”).ToArray();
var perWordValue=2f/words.Length;
foreach(单词中的var单词)
{
matchScoreInc=x=>matchScore.Invoke(x)+(x.Name.Contains(word)?perWordValue:0f);
matchScore=matchScoreInc.Expand();
}
}
变量q=
从表.AsExpandable()中的x开始
设y=matchScore.Invoke(x)
按顺序递减
选择新{Entity=x,MatchScore=y};
注意Invoke
、Expand
和AsExpandable的用法。
演示:
您可以将表达式与以下内容组合:
Expression<Func<Entity, float>> matchScore = x => 0;
Expression<Func<Entity, float>> matchScoreInc;
string s = "a b";
if (!string.IsNullOrWhiteSpace(s))
{
var words = s.Split(' ').ToArray();
var perWordValue = 2f / words.Length;
foreach (var word in words)
{
matchScoreInc = x => matchScore.Invoke(x) + (x.Name.Contains(word) ? perWordValue : 0f);
matchScore = matchScoreInc.Expand();
}
}
var q =
from x in table.AsExpandable()
let y = matchScore.Invoke(x)
orderby y descending
select new { Entity = x, MatchScore = y };
表达式匹配分数=x=>0;
表达公司;
字符串s=“a b”;
如果(!string.IsNullOrWhiteSpace))
{
var words=s.Split(“”).ToArray();
var perWordValue=2f/words.Length;
foreach(单词中的var单词)
{
matchScoreInc=x=>matchScore.Invoke(x)+(x.Name.Contains(word)?perWordValue:0f);
matchScore=matchScoreInc.Expand();
}
}
变量q=
从表.AsExpandable()中的x开始
设y=matchScore.Invoke(x)
按顺序递减
选择新{Entity=x,MatchScore=y};
注意Invoke
、Expand
和AsExpandable的用法。
演示:
正如我看到的matchScore
立即初始化,直到从数据库中检索数据时才会更改。因此,您可以编译matchScore
,并将返回的函数应用于从数据库中检索到的、下载到内存中的数据。你试过了吗?正如我所看到的那样,立即初始化,直到你从数据库中检索到数据才改变。因此,您可以编译matchScore
,并将返回的函数应用于从数据库中检索到的、下载到内存中的数据。你试过了吗?