[英]LINQ to SQL passing object as parameter
To select something with LINQ to SQL this is what we normally do: 要使用LINQ to SQL选择某些内容,这通常是我们要做的:
DataClassesDataContext context = new DataClassesDataContext();
var value = from employee in context.Employees
orderby employee.Salary descending
select employee;
What if i have like 30 table and i want to write this once in a method and only pass parameters to the function to call some table like this: 如果我有30个表并且想在方法中编写一次并且仅将参数传递给函数以调用某些表,该怎么办?
public void select_function(Parameter1,Parameter2)
{
DataClassesDataContext context = new DataClassesDataContext();
var value = from Parameter1 in context.Parameter2
orderby Parameter1.Salary descending
select Parameter1;
}
is that even possible ? 那有可能吗?
You can use GetTable<T>
generic method of LINQ to SQL Data Context object. 您可以对SQL数据上下文对象使用LINQ的
GetTable<T>
通用方法。 And pass an expression for sorting: 并传递一个用于排序的表达式:
public void select_function<T, TProperty>(Expression<Func<T, TProperty>> orderBy)
where T : class
{
DataClassesDataContext context = new DataClassesDataContext();
var value = context.GetTable<T>().OrderByDescending(orderBy);
}
Usage: 用法:
select_function((Employee e) => e.Salary)
Though you simply can do 虽然你可以做到
var value = context.GetTable<Employee>().OrderByDescending(e => e.Salary);
without creating any methods 没有创建任何方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.