简体   繁体   English

NancyFx超级简单视图引擎-替代Linq Enumerables

[英]NancyFx Super Simple View Engine - Substituting Linq Enumerables

I've found that if you expose a public property on your Model in SSVE of type IEnumerable<T> and return a Linq enumeration from that property, SSVE doesn't seem to think there are any values in that collection (checking @If.HasValues , so @Model.Values is the enumerable). 我发现,如果您在类型为IEnumerable<T> SSVE中对模型公开一个公共属性,并从该属性返回Linq枚举,则SSVE似乎认为该集合中没有任何值(检查@If.HasValues ,因此@Model.Values是可枚举的)。

For example, my C# property is: 例如,我的C#属性是:

public IEnumerable<string> Warnings
{
    get
    {
        return WarningGenerators.SelectMany(w => w.GetWarnings());
    }
}

In this case, the following html doesn't show up at all (it's not present in the DOM) in the generated page: 在这种情况下,以下html根本不会在生成的页面中显示(DOM中不存在):

@If.HasWarnings
<div class="row">
    <div class="col-lg-12">
        @Partial['WarningsWidget.sshtml', Model.Warnings]
    </div>
</div>
@EndIf

But if I change my C# to this (with no other changes) the html shows up: 但是,如果我将C#更改为此(不进行其他更改),则会显示html:

public IEnumerable<string> Warnings
{
    get
    {
        return WarningGenerators.SelectMany(w => w.GetWarnings()).ToList();
    }
}

Clearly the call to ToList() is enumerating the enumerable and the entries are then being picked up by SSVE. 显然,对ToList()的调用正在枚举枚举,然后SSVE会提取这些条目。 I'm surprised SSVE doesn't enumerate the collections passed into it though. 我很惊讶SSVE并未枚举传递给它的集合。

Is this behavior intended? 这是故意行为吗? Or have I missed something? 还是我错过了什么?

I don't know SSVE or Nancy that well, but I do know that sometimes LINQ and Entity Framework don't like when you only execute the query on the MVC page (which is what your first method is doing, as SelectMany() employs deferred execution ). 我不太了解SSVE或Nancy,但我确实知道有时LINQ和Entity Framework不喜欢只在MVC页面上执行查询(这是SelectMany()采用的第一种方法SelectMany() 推迟执行 )。 When your query is executed completely on the backend (as when .ToList() is used which employs immediate execution ), it tends to like that a lot better, as the items then are already loaded into memory. 当您的查询完全在后端执行时(例如,使用.ToList()时采用立即执行 ),它往往会好很多,因为这些项已经被加载到内存中了。

If that is what's going on, at least you have the comfort of knowing that it happens like this in other places as well. 如果这是怎么回事,至少您可以放心地知道它在其他地方也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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