gpt4 book ai didi

C# Linq 列名作为变量

转载 作者:太空狗 更新时间:2023-10-29 23:15:30 24 4
gpt4 key购买 nike

我有一个表,我想在其中对可变列进行查询。喜欢:

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**});
}
}

假设我想寻找供应商,那么它会是这样的:

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
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 列名作为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18762259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com