C# 带有数组输入和变量Where语句的LINQ查询-通知
我想使用Silverlight客户端API,通过WCF数据服务查询给定数组的数据以进行过滤。基本上,我想查询给定状态列表(数组)的员工 我是这样想的:C# 带有数组输入和变量Where语句的LINQ查询-通知,c#,linq,C#,Linq,我想使用Silverlight客户端API,通过WCF数据服务查询给定数组的数据以进行过滤。基本上,我想查询给定状态列表(数组)的员工 我是这样想的: public IQueryable<Employee> Load(string[] states) { foreach (var x in states) { // LINQ query here with 1 to N .Where statements return
public IQueryable<Employee> Load(string[] states)
{
foreach (var x in states)
{
// LINQ query here with 1 to N .Where statements
return from e in Context.Employees
.Where(...)
}
}
任何建议都将不胜感激 Context.Employees.ToList().Where(x=>states.Contains(x.State))我想这是一个可以运行的示例,它可以满足您的需要?给出一个州的列表,它会给你在这些州的雇员
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> states = new List<string>();
states.Add("SC");
states.Add("TX");
states.Add("NC");
List<Employee> emps = new List<Employee>();
emps.Add(new Employee() { State = "GA", Name = "Bill" });
emps.Add(new Employee() { State = "TX", Name = "John" });
emps.Add(new Employee() { State = "SC", Name = "Mary" });
//Here's where the work is done. The rest is fluff...
var empsinstates = from e in emps where states.Contains(e.State) select e;
foreach (var e in empsinstates)
{
Console.WriteLine(e.Name + " " + e.State);
}
Console.Read();
}
}
class Employee
{
public string State;
public string Name;
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
列表状态=新列表();
添加(“SC”);
添加(“TX”);
添加(“NC”);
List emps=新列表();
emps.Add(newemployee(){State=“GA”,Name=“Bill”});
emps.Add(newemployee(){State=“TX”,Name=“John”});
emps.Add(newemployee(){State=“SC”,Name=“Mary”});
//这是工作完成的地方,其余的都是绒毛。。。
var empsinstates=来自emps中的e,其中states.Contains(e.State)选择e;
foreach(美国的变量e)
{
Console.WriteLine(e.Name+“”+e.State);
}
Console.Read();
}
}
班级员工
{
公共字符串状态;
公共字符串名称;
}
}
您可以为条件动态构建表达式树
var parameter = Expression.Parameter(typeof(Employee), "employee");
Expression condition = Expression.Constant(false);
foreach (var state in states)
{
condition = Expression.OrElse(
condition,
Expression.Equal(
Expression.Property(parameter, "State"),
Expression.Constant(state)));
}
var expression = Expression.Lambda<Func<Employee, Boolean>>(condition, parameter);
我不能100%确定这是否适合您,但我希望总体思路能有所帮助。这不适用于EntityFramework或LINQ to SQL。如果参数是<代码> iQuestEng//C> >而不是“代码> iQueabd”,那么假设这是一个参数。如果您在Lead之前调用Telist-(),应该这样做。如果所有的雇员都被读取,如果这是一个大表,您可能需要考虑性能。然后过滤器应用程序他必须测试它,但我很确定EF和L2S都不能将复杂的对象(如列表)转换为匹配的SQL语句-至少在.NET 4.0之前。您的权利(我认为),但tolist会将其转换为普通的列表对象,因此,即使ef或L2不支持它,你也应该在这一点上处理L2o。运行筛选器时,已在数据库上执行查询。我会先在没有tolist的情况下测试它,因为如果它工作的话,效率会更高。您是使用LINQ to SQL还是实体框架还是纯LINQ?使用WCF数据服务(Astoria 1.0)和Silverlight 3.0(Silverlight客户端库)编辑原始post。这可以与LINQ to Object一起工作,但是(非常肯定)不支持实体框架或LINQ到SQL。这是我得到的:{将LINQ表达式转换为URI时出错:不支持方法“Contains”。}可能不是
或,而是OrElse
。和参数(类型(员工)…
谢谢,你说得对-我用字符串的列表编写代码进行测试,但忘了将其更改为Employee
。或
与或
之间的区别应该无关紧要,但人们当然通常会使用条件Or来代替正常的或ethod进行区分大小写的比较,而不是如何执行区分大小写的比较?Expression.Contains()方法不存在。
var parameter = Expression.Parameter(typeof(Employee), "employee");
Expression condition = Expression.Constant(false);
foreach (var state in states)
{
condition = Expression.OrElse(
condition,
Expression.Equal(
Expression.Property(parameter, "State"),
Expression.Constant(state)));
}
var expression = Expression.Lambda<Func<Employee, Boolean>>(condition, parameter);
var result = Context.Employees.Where(expression);