本文介绍了LINQ表达式来匹配多个批量投入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能写一个LINQ表达式来匹配多个批量投入?



这是我的数据库表

 会员
部,系
1 3
4月9日*
5 1
6 3 *
9 2

我要选择成员标有*



下面是我输入

  INT [] =师新INT [2] {4,6}; 
INT [] =部门新INT [2] {9,3};

下面是我试过



<$ P $查询p> VAR selectedMembers = members.Where(成员=> divisions.Contains(member.Division)及和放大器; department.Contains(member.Department))。ToArray的();

这不正确的行为EF。它与一个单一的输入,但不能同时输入(司和处)
应该返回对在输入
如匹配时:

 会员[] = selectedMembers新成员国[2] {新会员司{= 4,系= 9},新会员司{= 6系= 3}}; 


解决方案

此类型的过滤不直接由EF支持。



一个可能的解决方案是动态地建立这样一个判断:

 成员=> (member.Division ==区划[0]&放大器;&放大器; member.Department ==部门[0])
|| (member.Division ==区划[1]安培;&安培; member.Department ==部门[1])$ ​​B $ B ...
|| (member.Division ==区划[N-1]&放大器;&放大器; member.Department ==部门[N-1]);

下面是你如何做到这一点:

 变量参数= Expression.Parameter(typeof运算(会员),会员); 
VAR谓词= Expression.Lambda< Func键<会员,布尔>>(
Enumerable.Range(0,divisions.Length)
。选择(I =>新建表达式[]
{
Expression.Equal(Expression.Property(参数司),Expression.Constant(师[I])),
Expression.Equal(Expression.Property(参数处),Expression.Constant(部门由[i])),
}
.Aggregate(Expression.AndAlso))
.Aggregate(Expression.OrElse),
参数);
VAR的查询= members.Where(谓语);


How can I write a LINQ expression that will match multiple bulk inputs?

This is my database table

Members
Division, Department
1           3
4           9       *
5           1
6           3       *
9           2

I want to select the members marked with a *

Here are my inputs

int[] divisions = new int[2] {4, 6};
int[] department = new int[2] {9, 3};

Here's the query I tried

var selectedMembers = members.Where(member => divisions.Contains(member.Division) && department.Contains(member.Department)).ToArray();

This does not behave correctly in EF. It works when with a single input but not both inputs (Division and Department)It should return pairs matching in the inputseg:

Member[] selectedMembers = new Member[2] { new Member{Division=4,Department=9}, new Member{Division=6,Department=3}};
解决方案

This type of filtering is not supported directly by EF.

One possible solution is to build dynamically a predicate like this:

member => (member.Division == divisions[0] && member.Department == departments[0])
       || (member.Division == divisions[1] && member.Department == departments[1])
       ...
       || (member.Division == divisions[N-1] && member.Department == departments[N-1]);

Here is how you can do that:

var parameter = Expression.Parameter(typeof(Member), "member");
var predicate = Expression.Lambda<Func<Member, bool>>(
    Enumerable.Range(0, divisions.Length)
    .Select(i => new Expression[]
    {
        Expression.Equal(Expression.Property(parameter, "Division"), Expression.Constant(divisions[i])),
        Expression.Equal(Expression.Property(parameter, "Department"), Expression.Constant(departments[i])),
    }
    .Aggregate(Expression.AndAlso))
    .Aggregate(Expression.OrElse),
    parameter);
var query = members.Where(predicate);

这篇关于LINQ表达式来匹配多个批量投入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 07:18