Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

c# 4.0 - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...