2014-03-27 38 views
0

我必须把单选按钮的值动态成的Linq查询,因此该值在下面一个单选按钮的值的代码替换CUSTOMERNAME。这一切都住在实体框架的MVC项目中。那么如何在Linq查询中编写占位符?LINQ的占位符列名

public ActionResult Index(string whatToSearchFor, string radioButtonValue) 
{ 
    if (search == null) 
    { 
     var results = from p in db.Orders 
         select p; 

     ViewBag.Orders = results; 
     return View(); 
    } 
    else 
    { 
     var results = from p in db.Orders 
         where values 
         where p.customerName.StartsWith(search) 
         select p; 

     ViewBag.Orders = results; 
     return View(); 
    } 
} 

更新,什么工作对我来说:

public ActionResult Index(string whatToSearchFor, string radioButtonValue) 
{ 

    if(radioButtonValue == "NameOfTheRadioButton1") 
    { 
     var results = from p in db.Orders 
         where values 
         where p.column1.StartsWith(search) 
         select p; 

     ViewBag.Orders = results; 
     return View(); 
    } 
    else if(radioButtonValue == "NameOfTheRadioButton2") 
    { 
     var results = from p in db.Orders 
         where values 
         where p.column2.StartsWith(search) 
         select p; 

     ViewBag.Orders = results; 
     return View(); 
    } 
    else 
    { 
     var results = from p in db.Orders 
         select p; 

     ViewBag.Orders = results; 
     return View(); 
    } 
} 

回答

1

你将不得不使用查询语法使用lambda语法切换。

Expression<Func<Order, bool>> condition = null; 

if(rbFirst.Checked) 
    condition = o => o.FirstThing.StartsWith(search); 
else if(rbSecond.Checked) 
    condition = o => o.SecondThing.StartsWith(search); 
// etc... 

var results = db.Orders.Where(condition); 
+0

感谢您为我带来正确的方向。我刚刚检查了'radiobutton字符串值'的值,这对我很有用。我会更新我的帖子。 – oldsport