C#/ ASP.NET lambda转换 [英] C#/ASP.NET lambda conversion

查看:343
本文介绍了C#/ ASP.NET lambda转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我得到3个属性,其中一个的位置值为俄勒冈州。



但是在另一种方法声明如下:

  public ViewResult浏览(字符串位置) 
{
return View(db.Properties.Where(p => p.location == location).ToList());
}

我获得0个属性。



我怀疑字符串位置正在某处变得奇怪,但我不确定将会以什么方式进行转换。当我用'==替换lambda'== location'时,我仍然得到0个属性。



我如何解决这个问题? b
$ b

另外,还有看到为了调试而创建的中间SQL?



编辑:这是使用实体框架。



编辑:我现在得到轻微的不同结果。看来传入的位置字符串是空的(即使我在URL中看到它)。我已经重写了所有内容,并发布在:



传递字符串参数MVC 3

解决方案

我无法复制这个 - 我尝试了以下...

  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());
}

这两个都按预期工作。



创建了一个表格

  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]

这是使用现成的EF模型一代 - 没有T4模板,没有添加代码生成项目或其他任何东西。我建议你从头开始,一次重新构建一个东西,看看哪里出错了。



编辑:我刚添加了代码生成项目ADO .NET C#POCO实体生成器,结果没有变化。



要回答您的问题的第二部分,为了查看数据库查询,最简单的是挂起EFProf http://efprof.com/ - 30天试用应该适合您。


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.

这篇关于C#/ ASP.NET lambda转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆