C#Linq列名作为变量 [英] C# Linq Column Name as variable
本文介绍了C#Linq列名作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
喜欢:
private void query(string column,string value){
using (var db = new myDB()){
var s1 =(from c in db.Components
其中(**列** == **值**)
选择新的{c.id,**列**});
}
}
让我说我要找供应商将如下:
var s1 =(from c in db.Components
where(c.supplier == abc)
select new {c.id,c.supplier});
有没有办法将列名称作为变量传递?
解决方案
这个例子可以是有用的,我猜。
void BindGridTypeSafe ()
{
NorthwindDataContext northwind = new NorthwindDataContext();
var query = from p in northwind.Products
其中p.CategoryID == 3&&& p.UnitPrice> 3
orderby p.SupplierID
select p;
GridView1.DataSource = query;
GridView1.DataBind();
}
void BindGridDynamic()
{
NorthwindDataContext northwind = new NorthwindDataContext();
var query = northwind.Products
.Where(CategoryID = 3 AND UnitPrice> 3)
.OrderBy(SupplierID);
GridView1.DataSource = query;
GridView1.DataBind();
}
I have a table where I want to make a query on variable columns. Like:
private void query(string column, string value) {
using (var db = new myDB()) {
var s1 = (from c in db.Components
where (**column** == **value**)
select new {c.id, **column**});
}
}
lets say I want to look for a supplier then it would be like:
var s1 = (from c in db.Components
where (c.supplier == "abc")
select new {c.id, c.supplier});
is there a way to pass the column name as variable?
解决方案
This example can be useful i guess.
void BindGridTypeSafe()
{
NorthwindDataContext northwind = new NorthwindDataContext();
var query = from p in northwind.Products
where p.CategoryID == 3 && p.UnitPrice > 3
orderby p.SupplierID
select p;
GridView1.DataSource = query;
GridView1.DataBind();
}
void BindGridDynamic()
{
NorthwindDataContext northwind = new NorthwindDataContext();
var query = northwind.Products
.Where("CategoryID = 3 AND UnitPrice > 3")
.OrderBy("SupplierID");
GridView1.DataSource = query;
GridView1.DataBind();
}
这篇关于C#Linq列名作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文