简体   繁体   中英

C#/ASP.NET lambda conversion

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

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.

Here is the answer:

Passing string parameters MVC 3

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM