C#/ASP.NET lambda conversion

时光怂恿深爱的人放手 提交于 2019-12-14 03:27:29

问题


Somehow when I return this to my view:

return View(db.Properties.ToList());

I get 3 properties, one of which has the location value of "Oregon".

But in another method declared like this:

public ViewResult Browse(string location)
{
        return View(db.Properties.Where(p => p.location == location).ToList());
}

I get 0 properties.

I suspect the string "location" is getting converted to something strange somewhere, but I'm not sure in what way it would be getting converted. When I replace the lambda '== location' with '== "Oregon"' I still get 0 properties.

How do I fix this?

Also, is there anyway to see the intermediate SQL that gets created for debugging purposes?

EDIT: This is using entity framework.

EDIT: I'm getting slight different results now. It appears the incoming location string is empty (even though I see it in the URL). I have rewritten everything and posted it at:

Passing string parameters MVC 3


回答1:


I can't replicate this - I tried the following...

    public ActionResult Index()
    {
        var db = new ORMTestEntities();
        var oneprop = db.Properties.Where(p => p.location == "Oregon").ToList();
        ViewBag.Oneprop = oneprop;
        return View(db.Properties.ToList());
    }

    public ActionResult Index2()
    {
        var db = new ORMTestEntities();
        var oneprop = db.Properties.Where(p => p.location == "Oregon").ToList();
        ViewBag.Oneprop = oneprop;
        return View("Index",db.Properties.Where(p => p.location == "Oregon").ToList());
    }

both of which work as expected.

having created a table thus

CREATE TABLE [dbo].[Properties](
[Id] [int] IDENTITY(1,1) NOT NULL,
[location] [varchar](50) NULL,
[otherthing] [varchar](50) NULL,
 CONSTRAINT [PK_Properties] PRIMARY KEY CLUSTERED 
 (
[Id] ASC
 ) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,     ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
  ) ON [PRIMARY]

This is using the out-of-the-box EF model generation - no T4 templates, no "Add Code Generation Item" or anything else. I'd suggest you start from scratch and re-build one thing at a time to see where it goes wrong.

EDIT: I just added the code generation item "ADO.NET C# POCO Entity Generator" with no change in the results.

To answer the second part of your question, in order to see the DB queries the easiest thing is to hook up EFProf http://efprof.com/ - the 30 day trial should work for you.




回答2:


Here is the answer:

Passing string parameters MVC 3

Since the parameter was not named "id" everything went bad.



来源:https://stackoverflow.com/questions/6476254/c-asp-net-lambda-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!