asp.net mvc 3 EF HTML MultiSelect - 如何使用 LINQ 检索多个值

发布于 2024-11-10 07:04:37 字数 1656 浏览 0 评论 0原文

我有一个高级搜索表单,它包含输入框、下拉列表等。现在我想将下拉列表更改为多选列表。

我可以设置视图,如下所示:

@Html.ListBoxFor(model => model.IdState, null, new { size = "7" })

但我不知道如何将控制器从: 更改

public ActionResult Index(FormData model)
{
    IQueryable<mytable> results = from s in db.mytable.Include("State").where(s=> (model.IdState != null ? s.IdState == model.IdState : true)) select s;
    return View(result);
}

为多个 IdState 结果???

早些时候,IdState 是:

int IdState;

现在是:

IEnumerable<int> IdState;

感谢您抽出时间!

我仍然无法让它发挥作用。在 SQL 中将是:

SELECT * FROM MyTable WHERE IdState IN (1,2,5,7,10)

有什么想法吗? http://msdn.microsoft.com/en-us/vcsharp/aa336746。 aspx 不要显示类似的情况

解决方法

我在此讨论中找到:http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0 / 这段代码片段:

IQueryable<Foo> foos = context.Foo.Where("it.Id in {1, 2, 3, ...}");

我改为

string sState = "";
foreach (int a in model.IdState ) sState += (a.ToString() + ",");
sState = sState .Substring(0, sState .Length - 1);
IQueryable<mytable> results = from s in db.mytable.Where("it.IdState in {" + sState + "}");

And It Works Great!!!

希望能找到最终的解决方案

I have an Advanced Search form, It contains, input boxes, dropdown lists, etc. And now I want to change a dropdown list to a multiselect list.

I can set the view, like this:

@Html.ListBoxFor(model => model.IdState, null, new { size = "7" })

But I don't know how to change the controller from:

public ActionResult Index(FormData model)
{
    IQueryable<mytable> results = from s in db.mytable.Include("State").where(s=> (model.IdState != null ? s.IdState == model.IdState : true)) select s;
    return View(result);
}

To a multiple IdState Result???

Earlier the IdState was:

int IdState;

Now is:

IEnumerable<int> IdState;

Thanks for your time!

I still can't make it work. In SQL will be:

SELECT * FROM MyTable WHERE IdState IN (1,2,5,7,10)

Any Idea? The http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx Don't show similar situation

Workaround

I found on this discussion: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0/ this code snippet:

IQueryable<Foo> foos = context.Foo.Where("it.Id in {1, 2, 3, ...}");

I changed to

string sState = "";
foreach (int a in model.IdState ) sState += (a.ToString() + ",");
sState = sState .Substring(0, sState .Length - 1);
IQueryable<mytable> results = from s in db.mytable.Where("it.IdState in {" + sState + "}");

And It works great!!!

I hope can find the final solution

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如日中天 2024-11-17 07:04:37

而不是

s.IdState == model.IdState 

尝试使用

model.IdState.Contains(s.IdState)

Instead of

s.IdState == model.IdState 

try using

model.IdState.Contains(s.IdState)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文