0

I've a problem with linq methods of a List<T> where T is a custom class.

This is my class:

public class RoomWorkingPlan
{
        public Int64 m_IdRoom;
        public Dictionary<DateTime, List<WorkInterval>> workingPlan = new Dictionary<DateTime, List<WorkInterval>>();
}

I'm trying to use the methods any or select on a list like this :

List<RoomWorkingPlan> roomsworking = (List<RoomWorkingPlan>)m_RoomAvailable.Values.Cast<RoomWorkingPlan>().ToList();
DateTime startingDate = DateTime.now;

if (!roomsworking.Any<RoomWorkingPlan>(r => r.workingPlan.ContainsKey(startingDate)))
{
    return false;
}

but I received an error message saying "the expression cannot contains a Lambda Expression". Any ideas? On the http://msdn.microsoft.com all the examples contains Lamda expressions..

EDIT: m_RoomAvailable is an hashtable containing an int64 as key and RoomWorkingPlan as value.

JoeBilly
  • 3,057
  • 29
  • 35
MassiCiaoCiao
  • 74
  • 2
  • 6
  • This looks fine. I don't think that the exception is coming from this line. It sounds like an exception that would come from an ORM like Entity Framework or NHibernate. Please verify the line that throws the exception. Please show how `m_RoomAvailable` is initialized. – Daniel Hilgarth Apr 03 '13 at 10:38
  • What is `m_RoomAvailable` and why do you need to cast it? Btw, i think that you want `DateTime.Today` instead of `DateTime.Now`. – Tim Schmelter Apr 03 '13 at 10:40
  • I've edited my post with the definition of m_RoomAvailable. – MassiCiaoCiao Apr 03 '13 at 11:00
  • when do you get the exception. at runtime? on what row is the exception thrown? – default Apr 03 '13 at 11:03
  • The problem is not present in the code you posted. – Ken Kin Apr 03 '13 at 11:34
  • 1
    That's true, i'm sorry, doesn't give me an exeption, but he cannot evaluate the expression so it return always false even it have the date as key. I made a print for you but he's in italian. ![enter image description here][1] [1]: http://i.stack.imgur.com/wVNKd.png – MassiCiaoCiao Apr 03 '13 at 12:07
  • Are you saying that the program won't compile? If so, what line is the error on? – Jim Mischel Apr 03 '13 at 13:14

1 Answers1

0

You try to run lambda from the Quick Watch window (Controllo immediato, from your snapshot in your comment) in Visual Studio. Lambda expressions cannot be evaluated in this window : this is a limitation of Visual Studio. Lambda can only be evaluated in code at runtime.

It is also true for the Watch, Locals and Immediate windows.

If you want to debug lambdas, you have to use breakpoints in your code. Note you'll only have access to your functors (predicates, selectors...) and the result.

For more informations : Visual Studio debugging "quick watch" tool and lambda expressions

Community
  • 1
  • 1
JoeBilly
  • 3,057
  • 29
  • 35