C# 如何使用select from表中的字符串?

C# 如何使用select from表中的字符串?,c#,entity-framework,C#,Entity Framework,如何使用select from表中的字符串? 例如: private void Select(string strKind,int Rank, string Blah) { var strWhere =""; if(strKind == "RANK") { strWhere = "where P.IDRANK ==" + Rank; }

如何使用select from表中的字符串? 例如:

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;
        }
转换此查询:

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

如果您可以获得一个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;
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;
        }
//构建数据表并添加列
DataTable dt=新的DataTable();
dt.列添加(“IDRank”);
dt.列。添加(“废话”);
//向表中添加行
对于(int i=1;i<5;i++){
DataRow dr=dt.NewRow();
dr[“IDRank”]=i;
dr[“Blah”]=“Blah”+i.ToString();
dt.Rows.Add(dr);
}           
//获取表的数据视图
DataView dv=dt.AsDataView();
//定义过滤器
字符串过滤器=“IDRank=2”;
//应用过滤器
dv.RowFilter=过滤器;
//运行查询
var Query1=从dv.ToTable()中的P开始。AsEnumerable()选择P;

每个表,我都在编写很多函数。我需要将这些函数转换为一个函数。
例如:每个表有两个函数

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;
        }
现在,我将转换为一个函数。例如

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;
        }
好吗?

我使用这个代码

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;
        }
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();
            }
使用(var context=new DBEntities())
{
System.Data.Objects.ObjectQuery contacts=context.Users.Where(“it.FirstName=@fname”,new System.Data.Objects.ObjectParameter(“fname”,“Arash”);
列表项=contacts.ToList();
}

Use PredicateBuilder:您能解释一下为什么要这样做吗?强类型语法应该是LINQ查询的一个加号。因为我想要所有程序,请选择一个。请提供更多详细信息。