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 を使用して単一の文字列に変換します) を返してもらいたいです。クエリによってこれら 2 つの項目が返されたら、ビュー モデルのコンストラクターを使用して変換し、DataTable を使用してすべてのデータを適切に表示できるようにします。私が探している答えはおそらく非常に具体的ですが、IEnumerableを返したいときにサブクエリエラーが発生する理由とその修正方法を理解する必要があります。また、注意してください... .netドキュメントによると、コードは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)));

これは、空の文字列であるコードを含めずNotificationConfigurationに toに変換します。NotificationConfigurationViewModel

于 2013-02-25T15:07:44.113 に答える
0

答えはデータセットではなくクエリにあるので、正確な答えを出すにはクエリを確認する必要があります。ただし、一般的に、使用できる構成はいくつかあります。おそらく、WHERE Id =(SELECT ID FROM Values WHERE DateOFValues BETWEEN FirstDate AND LastDate)のようなものがあります。必要なのは、WHERE ID IN (SELEct...またはWHEREIS = ANY(SELECT...またはサブクエリ。この場合はこちらをご覧ください...アクセスと表示されていることはわかっていますが、MSSQLはここで使用されているすべての構文をサポートしています。

于 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 に答える