var range = this.dataStore.Query<NotificationConfiguration>()
.Range(p => new NotificationConfigurationViewModel(p, from x in p.Events where !(x.Code == null || x.Code.Equals("")) select x.Code), pageNumber);
return this.View(range);
我有上面的代码,我想在其中返回一个 IEnumerable 但得到一个“子查询返回超过 1 个值。当子查询遵循 =、!=、<、<=、>、>= 或子查询时,这是不允许的每次运行代码时都用作表达式。我知道查询只想返回一个值,但是如何让它按预期返回一组值?请帮忙!
让我澄清一下......我希望范围包括一个新对象。我有一个正在通过数据库的查询。它返回数据,然后我使用以下视图模型构造函数进行转换:
public NotificationConfigurationViewModel(NotificationConfiguration notification , IEnumerable<string> codes)
{
Contract.Requires(notification != null);
this.notification = notification;
this.codes = codes;
}
每个通知配置都有属性,然后是与其相关的事件列表。我只需要所述列表中的代码。
只是为了再次澄清。我希望查询返回一个 NotificationConfiguration 和一个 IEnumerable(稍后我将使用 SB 将其转换为单个字符串)。一旦查询将这两项返回给我,我将使用视图模型中的构造函数对其进行转换,以便可以使用 DataTable 正确显示所有数据。我正在寻找的答案可能非常具体,但是当我希望它返回 IEnumerable 以及如何修复它时,我需要了解为什么我会收到子查询错误。另请注意...根据.net docs,代码应该交给我一个 IEnumerable 但由于某种原因它仍然崩溃。这是相关的代码示例:
[HttpPost]
public ActionResult Index(DataTableRequest requestedData)
{
using (this.dataStore.Session.BeginTransaction())
{
return this.dataStore.Query<NotificationConfiguration>()
.TableRange(requestedData, p => new NotificationConfigurationViewModel(p, from x in p.Events select x.Code));
}
}
.
public NotificationConfigurationViewModel(NotificationConfiguration notification , IEnumerable<string> events)
{
Contract.Requires(notification != null);
this.notification = notification;
this.events = events;
}
.
[Display(Name = "Events")]
public virtual string EventTypeCodes
{
get
{
var codes = new StringBuilder();
foreach (var item in this.events)
{
codes.Append(item + ",");
}
return codes.ToString().TrimEnd(',');
}
}