0

次のlinqクエリを実行しました

var results = from myRow in QCAllDataSet.DataTable1.AsEnumerable()
                                      where myRow.Field<String>("ESRNumber") == value
                                      select new ESR(myRow.ESRNumber,
                                                           myRow.CreationDate,
                                                           myRow.Subsystem,
                                                           myRow.Name,
                                                           myRow.Product,
                                                           myRow.Version,
                                                           myRow.ESRStatus,
                                                           myRow.Owner,
                                                           myRow.Priority,
                                                           myRow.LastPHNote,
                                                           myRow.LastPHNoteDate,
                                                           myRow.LastPHNoteUser,
                                                           myRow.DaysFromLastUpdate,
                                                           myRow.Customer,
                                                           myRow.T2Owner,
                                                           myRow.T2Group,
                                                           myRow.comment,
                                                           myRow.ESRAge);

そして、オブジェクトに変換resultsしたいESR

のようなsomthigESR t=(ESR)results

しかし、次のエラーが発生します。

タイプ'System.Data.EnumerableRowCollectionをESRに変換できません

これをどのようにCasする必要がありますか?

4

3 に答える 3

1

FirstOrDefault結果から 1 つの項目のみを取得するために使用します。

var esrObject=  (from myRow in QCAllDataSet.DataTable1.AsEnumerable()
 where myRow.Field<String>("ESRNumber") == value
  select new ESR(myRow.ESRNumber,
                  myRow.CreationDate,
                  myRow.Subsystem)).FirstOrDefault();

コレクションが 1 つのレコードのみを返すことが確実な場合に使用できますSingleOrDefault 。それ以外の場合は、FirstOrDefault を使用します。FirstOrDefaultシーケンスの最初の要素を返すか、シーケンスに要素が含まれていない場合はデフォルト値を返します。したがって、コレクション式が複数の行を返す場合でも心配する必要はありません。

于 2012-04-26T22:20:21.520 に答える
1

LINQ を使用するselectと、単一の結果のみを取得する場合でも、コレクションが返されます。単一の結果を示すには、Single()LINQ メソッドを使用します。

ESR t = results.Single();

結果が返されない (一致しない) 可能性がある場合はSingleOrDefault()、null を使用して確認できます。

ESR t = results.SingleOrDefault();
if (t == null)
    // could not find match
于 2012-04-26T22:20:23.680 に答える
1

An Enumerable<T> is not a single object, even if it contains only one. You have to specify what you want. There are plenty of extension methods in Enumerable like:

For example:

List<ESR> allESR = results.ToList();
ESR firstESR = results.First(); // throws an exception if there is not at least one
ESR firstESR = results.FirstOrDefault(); // returns null if there's not at least one

Note that the query is not executed until you call one of these methods(or use a foreach to iterate them) due to LINQ's deferred execution.

于 2012-04-26T22:26:26.420 に答える