Question : Problem with Linq query - undesired casting of result type to WhereEnumerableIterator

Hi,

I have a LINQ casting problem for DateTime types.

This query extracts all but the minimum (or it exclude the first) value from a table I have in C# code. This is what the table is like (Real data is provided in the attached word doc):

Id      F1s
---      ---
1      2009-08-12 04:00:00.000
1      2009-08-12 10:00:00.000
1      2009-08-13 01:00:00.000
1      2009-08-13 11:00:00.000

2      2009-08-13 17:00:00.000
2      2009-08-13 23:00:00.000
2      2009-08-14 01:00:00.000
2      2009-08-14 05:00:00.000



var qrylinq = (from lstRow1 in datatable2.AsEnumerable()
                     group lstRow1 by lstRow1.Field(0)
                      into lRow1
                      let MinF1 = lRow1.Min(lvalue => lvalue.Field("F1s"))      
                                                 
                      select new
                      {

                      newF1s = lRow1.Select(lvalue => lvalue.Field("F1s")).Where(lvalue => lvalue != MinF1 ),
                                                     
                      }
               );


                           
              for (int i = 0; i < qrylinq.Count(); i++)
                 {
                   DateTime dtt = Convert.ToDateTime(qrylinq.ElementAt(i).newF1s);
                      }


I get the exception....

"Unable to cast object of type 'WhereEnumerableIterator`1[System.DateTime]' to type 'System.IConvertible'." The query seems to cast the results as an object of  a base class 'WhereEnumerableIterator' even though the final results of the query are 'DateTime?' (nullable). It usually allows me to convert DateTime? to DateTime. How do I convert my result to a DateTime in this case where this cast to WhereEnumerableIterator occurs?

See attached file for real data. Please post original solutions. I will respond to all questions ASAP.

Answer : Problem with Linq query - undesired casting of result type to WhereEnumerableIterator

Hi rt2001;

The query in the code snippet below will return the following results: It also has how you may go about getting the difference between the two.

Group By ID 1
      newFCOffs
      Date and time = 2009-08-12 16:00:00.000
      Date and time = 2009-08-13 03:20:00.000
      Date and time = 2009-08-13 11:00:00.000
      newFCOns
      Date and time = 2009-08-12 10:20:00.000
      Date and time = 2009-08-12 19:10:00.000
      Date and time = 2009-08-13 09:00:00.000
=====
Group By ID 2
      newFCOffs
      Date and time = 2009-08-14 05:00:00.000
      Date and time = 2009-08-14 13:00:00.000
      Date and time = 2009-08-14 17:00:00.000
      newFCOns
      Date and time = 2009-08-13 20:00:00.000
      Date and time = 2009-08-14 09:00:00.000
      Date and time = 2009-08-14 14:00:00.000
=====

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
var qrylinq = (from lstRow1 in dta2.AsEnumerable()
               group lstRow1 by lstRow1.Field("Ids")
                   into lRow1
                   select new
                   {
                       Key = lRow1.Key,
                       LessFirst = lRow1.OrderBy(d => d.Field("newFCOffs")).Skip(1),
                       LessLast = lRow1.OrderBy(d => d.Field("newFCOns")).Take( lRow1.Count() -1)
                   }).ToList();

// How to go about your calculations
for (int i = 0; i < qrylinq.Count(); i++)
{
    DataRow[] drFCOffs = qrylinq[i].LessFirst.ToArray();
    DataRow[] drFCOns = qrylinq[i].LessLast.ToArray();
    
    for( int j = 0; j < drFCOffs.Length; j++)
    {
        Console.WriteLine("Difference {0}", drFCOffs[j].Field("newFCOffs") - drFCOns[j].Field("newFCOns"));
    }
}

// Just to display the query results
foreach (var gup in qrylinq)
{
    Console.WriteLine("Group By ID {0}", gup.Key);
    Console.WriteLine("\tnewFCOffs");
    foreach (var dr in gup.LessFirst)
    {
        Console.WriteLine("\tDate and time = {0}", dr.Field("newFCOffs").ToString("yyy-MM-dd HH:mm:ss.fff"));
    }
    Console.WriteLine("\tnewFCOns");
    foreach (var dr in gup.LessLast)
    {
        Console.WriteLine("\tDate and time = {0}", dr.Field("newFCOns").ToString("yyy-MM-dd HH:mm:ss.fff"));
    }

    Console.WriteLine("=====");
}
Random Solutions  
 
programming4us programming4us