这是我的阵列输出
Array
(
[1] => 1
[2] => 2
[3] =>
)
Run Code Online (Sandbox Code Playgroud)
我怎么知道它[3] =>是空的?
foreach ($array as $key => $value) {
if (empty($value))
echo "$key empty <br/>";
else
echo "$key not empty <br/>";
}
Run Code Online (Sandbox Code Playgroud)
我的出局表明并非空洞.什么是正确的检查方法是空的?
我有一个我使用的所有不同帐户名称前缀(az)的列表
var accounts = this.SessionManager.GetActiveSession().QueryOver<Account>();
var q = accounts.Select(Projections.Distinct(
Projections.SqlFunction("substring",
NHibernateUtil.String,
Projections.Property("Name"),
Projections.Constant(1),
Projections.Constant(1))));
Run Code Online (Sandbox Code Playgroud)
但是,我想要做的不是返回一个不同的列表,而是将前缀分组,并返回以该前缀开头的帐号数,但我不确定如何使用查询来执行组,因为它不像标准那样简单LINQ.
我使用QueryOver而不是Query的原因是因为某些原因,子串函数正在内存中执行而不是在数据库服务器上执行.
这就是我通常会这样做的方式
var prefixes = (from acc in this.SessionManager.GetActiveSession().Query<Account>()
group acc by acc.Name.Substring(0, 1)
into grp
select new
{
Prefix = grp.Key,
Count = grp.Count()
});
Run Code Online (Sandbox Code Playgroud)
编辑这是我尝试但我收到以下错误
表达式中无法识别的方法调用SqlFunction("substring",NHibernateUtil.String,new [] {Property("Name"),Constant(Convert(1)),Constant(Convert(1))})
var accounts = this.SessionManager.GetActiveSession().QueryOver<Account>().Select(
Projections.Group<string>(x => Projections.SqlFunction("substring", NHibernateUtil.String,
Projections.Property("Name"), Projections.Constant(1),
Projections.Constant(1))),
Projections.Count<string>(x => Projections.SqlFunction("substring", NHibernateUtil.String,
Projections.Property("Name"), Projections.Constant(1),
Projections.Constant(1)))
);
Run Code Online (Sandbox Code Playgroud) 我正在MVC中构建一个站点,我传递给我的View的View Model包含一个自定义对象,该对象又包含一个IEnumarable自定义对象列表.
这个想法是剃刀将动态生成IEnumerable的表单,可以是任意数量的对象.
@foreach (var data in Model.Kpi.Values)
{
<div class="editor-label">
@Html.Label(data.Field.Name);
</div>
<div class="editor-field">
@Html.EditorFor(model => data.Value)
@Html.ValidationMessageFor(model => data.Value)
</div>
}
Run Code Online (Sandbox Code Playgroud)
表单显示完美,包括数据注释,但IEnumerable在控制器post函数中为null.
[HttpPost]
public virtual ActionResult Create(KpiCreateViewModel vm)
{
return this.RedirectToAction(MVC.Kpi.Index());
}
Run Code Online (Sandbox Code Playgroud)
我在return语句上加了一个断点,并检查了vm变量的内容.
任何人都可以建议一种方法来检索表单数据?
提前致谢
我正在尝试使用DataContractJsonSerializer类将对象列表输出为json格式,但是我一直遇到以下错误.
Type 'Castle.Proxies.JokeCategoryProxy' with data contract name
'JokeCategoryProxy:http://schemas.datacontract.org/2004/07/Castle.Proxies'
is not expected. Consider using a DataContractResolver or add any types not
known statically to the list of known types - for example, by using the
KnownTypeAttribute attribute or by adding them to the list of known
types passed to DataContractSerializer.
Run Code Online (Sandbox Code Playgroud)
我知道这已经得到了回答,但只有在我的对象中有一个属性是另一个自定义对象时才会发生.
[DataContract]
[KnownType(typeof(ModelBase<int>))]
public class Joke : ModelBase<int>
{
[DataMember]
public virtual string JokeText { get; set; }
[DataMember]
public virtual JokeCategory JokeCategory { get; set; }
}
[DataContract]
[KnownType(typeof(ModelBase<int>))]
public …Run Code Online (Sandbox Code Playgroud) .net c# fluent-nhibernate datacontractserializer known-types