1
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(',');
        }
    }
4

3 回答 3

2

这里的问题是Range方法(复制如下)。

.Range(p => new NotificationConfigurationViewModel(p, from x in p.Events where !(x.Code == null || x.Code.Equals("")) select x.Code), pageNumber);

Range期望调用者传入以下参数:

public static IEnumerable<int> Range(
    int start,
    int count
)

代码正在传入(NotificationConfigurationViewModel, int)。这当然似乎是问题的一部分。我相信适当的解决方案如下:

var range = from p in this.dataStore.Query<NotificationConfiguration>()
select new NotificationConfigurationViewModel(p, p.Events.Where(x => !string.IsNullOrEmpty(x.Code)));

这将从您的转换NotificationConfigurationNotificationConfigurationViewModel,而不包括任何空字符串的代码。

于 2013-02-25T15:07:44.113 回答
0

答案在查询中,而不是数据集中,所以要给出准确的答案,我们需要查看查询。但是,一般来说,您可以使用一些构造。您可能有类似 WHERE Id = (SELECT ID FROM Values WHERE DateOFValues BETWEEN FirstDate AND LastDate) 之类的东西。你想要的是 WHERE ID IN (SELECT ... OR WHERE IS = ANY (SELECT ... 或子查询在这种情况下看这里... http://allenbrowne.com/subquery-01.html(是的,我知道它说访问,但 MS SQL 支持此处使用的所有语法。

于 2013-02-25T15:09:05.147 回答
0

继续假设......这就是你所追求的吗?

如果您尝试创建一堆新对象,则最好在查询的选择部分内完成对象创建。

int pageNumber = 0;
int pageSize = 10;

IQueryable<NotificationConfiguration> configurations = this.dataStore.Query<NotificationConfiguration>();

IList<NotificationConfigurationViewModel> viewModels =
    (from configuration in configurations
     where !string.IsNullOrEmpty(configuration.Code)
     select new NotificationConfigurationViewModel(configuration))
    .Skip(pageNumber * pageSize).Take(pageSize).ToList();

return viewModels;
于 2013-02-25T15:16:24.803 回答