Search code examples
c#linq2sxc

.Where in list gives error for using dynamic types in 2sxc


I have this working code:

@{
      var ekgList = AsList(App.Data["Ekgs"]);
      foreach(var ekg in ekgList) {
          <div>
              @foreach (var entitiesFromSinusDataType in ekg.Sinus) {
                  if(entitiesFromSinusDataType.EntityId == Content.EntityId) {
                      <a>@ekg.ShortName</a>
                  }
              }
          </div>
     }
}

For what I can understand:

  • ekgList is a list (standard 2sxc code for accessing data)
  • ekg.Sinus is a field in Ekgs content type (Entity data type, multiple, which creates a list of entities from another content type, Sinus)
  • Content.EntityId is an int, the same as entitiesFromSinusDataType.EntityId

There should be an easy way to remove the second loop and the "if", by placing a where clause in the first loop. I'm trying this step by step, but as soon as I try this:

@{
      var ekgList = AsList(App.Data["Ekgs"]);
      foreach(var ekg in ekgList) {
          <div>
              @foreach (var entitiesFromSinusDataType in ekg.Sinus.Where(i => i.EntityId == Content.EntityId)) {
                  <a>@ekg.ShortName</a>
              }
          </div>
     }
}

I get this error:

CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

This seems to be caused by using dynamic types.

Is there some way to cast the lists into a non dynamic version of them?


Solution

  • Try to cast the ekg.Sinus to ((IEnumerable<dynamic>)ekg.Sinus) :

    ((IEnumerable<dynamic>)ekg.Sinus).Where(i => i.EntityId == Content.EntityId)
    

    I hope this will help you out.