2012-12-07 43 views
-1
Func<vw_UsuarioPerfilAtributo, bool> expressionPerfil = Perf => 
    foreach(int _key in Keys){ 
    Perf.Id == _key || 
    } 

不同ID的变量数(我需要多个或||LAMBDA更快的方式对搜索

我觉得这是比

List<vw_UsuarioPerfilAtributo> teste = new List<vw_UsuarioPerfilAtributo>(); 
    teste.add(context.Find(Id)); 

可能更快?

+4

一个布尔值,你能提供更多的上下文返回找到的元素? –

+0

您可以调查“分割”模式。 –

回答

1

也许你需要的东西,如:

//use a method because use a foreach in a lambda expression isn't allowed 
public bool myFunction(vw_UsuarioPerfilAtributo Perf){ 
    foreach(int _key in Keys){ 
     if(Perf.Id == _key || /*other condition here*/) 
      return true; 
    } 

    return false; 
} 

而且:

Func<vw_UsuarioPerfilAtributo, bool> expressionPerfil = Perf => myFunction(Perf); 

或者只是:

Func<vw_UsuarioPerfilAtributo, bool> expressionPerfil = Perf => Keys.Any(_key => Perf.Id == _key || /*other condition here*/); 

我觉得这是快于

teste.add(context.Find(同上));

在这种情况下context.Find(id)(其中上下文是List<>),而前面的代码返回因为Func<vw_UsuarioPerfilAtributo, bool>

+1

Keys.Any,太棒了,我会明天测试,所以给一个反馈。谢谢! –

+0

对不起,我得到的假期,但keys.Any Wokrs非常好! 但关于第一个,返回有问题,我试图创建一个布尔的列表,但只需要投给一个......所以我不能让这个第一种形式,看问题是只有一次返回行为。谢谢!问题解决白衣第二选择。 –

+0

哦,对不起,这是我的错。现在它应该工作。 –