如何在LINQ to ENTITIES中使用变量select语句?

时间:2012-03-26 09:59:52

标签: asp.net linq-to-entities

我想从名为“Customer”的表中选择一些行,但是当我使用“db.Customer”定义tableName时似乎错了。我有什么方法可以做到吗?

            string tableName = "db.Customer";
            var result = from p in tableName 
                     select new
                     {
                         ID = p.ID,
                         Des = p.Description,
                         type = p.Type,
                         date = p.DateCreated

                     };
        gridview.DataSource = result;
        gridview.DataBind();

1 个答案:

答案 0 :(得分:0)

您可以使用CreateQuery方法(接收实体SQL命令)

var result = ctx.CreateQuery<DbDataRecord>(@"select p.ID, 
                                                        p.Description,
                                                        p.Type, 
                                                        p.DateCreated 
                                                 from Customer as p");

ExecuteStoreQuery(执行T-SQL)

   var result = ctx.ExecuteStoreQuery<CustomerInfo>(@"select p.ID, 
                                                            p.Description,
                                                            p.Type, 
                                                            p.DateCreated 
                                                     from dbo.Customer as p");


public class CustomerInfo
{
   public int ID {get; set;}
   public string Description {get; set;}
   public int Type {get; set;}
   public DateTime DateCreated {get; set;}
}