如何使用select from table中的字符串? 例如:
转换此查询:
DBEntities MyDB = new DBEntities();
var Query1 = from P in MyDB.Per
where P.IDRANK == 2
select P;
to:
string strquery = "where P.IDRANK == 2";
DBEntities MyDB = new DBEntities();
var Query1 = from P in MyDB.Per
strquery
select P;
答案 0 :(得分:0)
如果你能得到一个DataTable,你可以这样做:
// Build a data table and add columns
DataTable dt = new DataTable();
dt.Columns.Add("IDRank");
dt.Columns.Add("Blah");
// Add rows to the table
for (int i = 1; i < 5; i++) {
DataRow dr = dt.NewRow();
dr["IDRank"] = i;
dr["Blah"] = "Blah" + i.ToString();
dt.Rows.Add(dr);
}
// Get a DataView to table
DataView dv = dt.AsDataView();
// Define the filter
string filter = "IDRank = 2";
// Apply the filter
dv.RowFilter = filter;
// Run the query
var Query1 = from P in dv.ToTable().AsEnumerable() select P;
答案 1 :(得分:0)
每张桌子,我写了很多功能。我需要将这些转换为一个功能 例如:我有两个Table Per
函数private void SelectByRank(int Rank)
{
DBEntities MyDB = new DBEntities();
var Query1 = from P in MyDB.Per
where P.IDRANK == Rank
select P;
}
private void SelectByRankANDBlah(int Rank, string Blah)
{
DBEntities MyDB = new DBEntities();
var Query1 = from P in MyDB.Per
where P.IDRANK == Rank && P.BLAH == Blah
select P;
}
现在,我将转换为一个函数。例如
private void Select(string strKind,int Rank, string Blah)
{
var strWhere ="";
if(strKind == "RANK")
{
strWhere = "where P.IDRANK ==" + Rank;
}
else
{
strWhere = "where P.IDRANK == " + Rank + " && P.BLAH == " + Blah;
}
DBEntities MyDB = new DBEntities();
var Query1 = from P in MyDB.Per
strWhere
select P;
}
OK?
答案 2 :(得分:0)
我使用此代码
using (var context = new DBEntities())
{
System.Data.Objects.ObjectQuery<User> contacts = context.Users.Where("it.FirstName = @fname", new System.Data.Objects.ObjectParameter("fname", "Arash"));
List<User> items = contacts.ToList();
}