1

I have two objects declared as IEnumerable<DateTime> xVal and IEnumerable<double> yVal. Then some where is my program I have:

var xVals = from p in result where p.chemical == "Benzene" select p.SampDate_Name;
var yVals = from p in result where p.chemical == "Benzene" select p.PCL;

Then I am trying to the following assignment:

 xVal = xVals as IEnumerable<DateTime>;
 yVal = yVals as IEnumerable<double>;

And the above code sets xVal and yVal to null.

Could anybody explain what is wrong here? I would very much appreciate the help.

Thanks.


One thing is certain: The input is not null. So xVals and yVals are non-null as debugged by me. I am also able to loop through them to get the content of each. I will try the other suggestions when I get back and post my findings. Thanks a lot folks for your replies.

The problem here was that LINQ is executed lazy, It will be executed when you do either foreach or ToList/ToArray/ToDictionary. The two linq queries in my code were not executed before the assignment was done. Consequently xVal and yVal were set to null. Thanks for all the help.

  • When you mouse over the `var` keywords, what does it say? – AakashM Sep 02 '11 at 22:34
  • What does the debugger tell you for (xVals as IEnumerable).ToString(); ? – gsvolt Sep 02 '11 at 21:30
  • Hovering over the var keywords tells this: IEnumerable and IEnumerable respectively. So the debugger is casting to correct type while executing the Linq. So my question is what does it mean by IEnumerable... this seems to be the issue. – Suhartha Chowdhury Sep 06 '11 at 19:27
  • (xVals as IEnumerable).ToString() throws a run time error saying that "Object reference not set to an instance of an object". I have no clue why it is so when xVals is set to the result of the Linq query. – Suhartha Chowdhury Sep 06 '11 at 19:57

3 Answers3

3

The as keyword will set the output to null if the input was null or the cast could not be carried out.

If you used xVal = (IEnumerable<DateTime>)xVals you would probably get an exception saying that it could not be cast.

MSDN description for as keyword

Fiona - myaccessible.website
  • 14,481
  • 16
  • 82
  • 117
1

If you have a generic collection, to get it to IEnumerable, try .AsEnumerable(). So your new code...

xVal = xVals.AsEnumerable();
yVal = yVals.AsEnumerable();
EtherDragon
  • 2,679
  • 1
  • 18
  • 24
  • I tried to do this... but the compiler throws an error saying: cannot implicitly convert type ...IEnumerable to ...IEnumerable. An explicit conversion exists. – Suhartha Chowdhury Sep 06 '11 at 19:36
  • DateTime? is not the same as DateTime, this is probably why your other cast attempts have returned null, because they are not the same type. You could try using xVal = xVals as IEnumerable; and see if you get better results. – EtherDragon Sep 06 '11 at 23:02
0

The as keyword doesn't actually do a cast, but does conversions between related reference types. So if you have a concrete type, you could treat assign it to a new variable with a type of the base class using as. If the types are not compatible, then as will return null. This is a good way to do type checking without having to worry about exceptions. Instead you can perform a null check on the result of the conversion.

It's hard to tell what types p.SampDate_Name and p.PCL are, but I would guess strings? If so you can add the cast to the select statement.

var xVals = from p in result where p.chemical == "Benzene" select DateTime.Parse(p.SampDate_Name);
var yVals = from p in result where p.chemical == "Benzene" select Double.Parse(p.PCL);
NerdFury
  • 18,876
  • 5
  • 38
  • 41
  • 2
    The `as` operator *does* perform a cast (if possible). And the advantage is not type checking without throwing exceptions (afterall, that's what the `is` operator is for), but rather that you can do the cast and the type check in one operation, which is more efficient. Furthermore, you don't even need to use the `as` operator when assigning a variable from a concrete type to the base type. – Kirk Woll Sep 02 '11 at 22:40