0

I did the following linq query

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);

And I would like to convert results to ESR Object

somthig like ESR t=(ESR)results

but I get the following error:

Cannot convert type 'System.Data.EnumerableRowCollection to ESR

How should I Cas this?

MoShe
  • 5,937
  • 14
  • 45
  • 73

3 Answers3

1

Use FirstOrDefault to get only one item from the results.

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

You can use SingleOrDefault when you are sure your collection will return only one record, Otherwise use FirstOrDefault. FirstOrDefault will return first element of a sequence, or a default value if the sequence contains no elements. So you dont need to worry even if your collection expression returns more than one row.

Shyju
  • 206,216
  • 101
  • 402
  • 492
1

When you select with LINQ you are getting back a collection, even if you intend to retrieve only a single result. To indicate a single result, use the Single() LINQ method:

ESR t = results.Single();

If it could possibly return no results (no matches), then you can use SingleOrDefault() and check for null:

ESR t = results.SingleOrDefault();
if (t == null)
    // could not find match
mellamokb
  • 55,194
  • 12
  • 105
  • 134
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.

Community
  • 1
  • 1
Tim Schmelter
  • 429,027
  • 67
  • 649
  • 891