【发布时间】:2018-06-28 08:40:06
【问题描述】:
public ActionResult Query(List<string> sortable_bot, List<string> sortable_top)
{
string gro = "from st in context.Customers";
for (int i = 0; i < sortable_top.Count; i++)
{
gro += " group st by st." + sortable_top[i].ToString();
}
string into = " into g select new Group2() { Key = g.Key, Count = g.Count() }";
gro += into;
using (var context = new NwContext())
{
*var que = gro;
return View(que.ToList());
}
}
我在星号处放了一个断点,gro 字符串等于from st in context.Customers group st by st.Country into g select new Group2() { Key = g.Key, Count = g.Count() }
但这不起作用。如果我像这样直接写var que= from st in context.Customers group st by st.Country into g select new Group2() { Key = g.Key, Count = g.Count() };,那就行了。
【问题讨论】:
-
在字符串中执行 linq 似乎不正确...
*var应该是什么?即使使用动态 linq,您也有单独的方法.Where(string), .Select(string)... -
@xanatos 了解我放置断点的位置
-
你不能把 C# 代码放在一个字符串中,然后像代码一样调用它。尤其是 Linq,它首先被翻译成 C# 语句,然后然后被编译成可运行的代码。你为解决你试图解决的实际问题而采取的方法并不能解决问题,所以你有一个 XY 问题。解释您的实际问题并找到不同的方法。
-
@CodeCaster 你知道另一种解决方案吗?
-
如果您不解释您要解决的实际问题,则不会。
标签: .net asp.net-mvc linq