Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sean Tomlins 9 posts 29 karma points
    Oct 14, 2011 @ 01:05
    Sean Tomlins
    0

    Custom Data Types and Where Clause

    I have a custom data type called "location" that has cityid and regionid as properties

    I'd like to create a Where clause that will filter on cityid and regionid

    @Model.Children.Where("location.cityid == someNumber") doesn't work

    Is there currently a way to do this?

    P.s. I didn't create this custom data type, please don't flame me lol.

  • Sean Tomlins 9 posts 29 karma points
    Oct 14, 2011 @ 05:08
    Sean Tomlins
    0

    After many hours of bashing my head against the wall, I came up with an answer using ConvertAll<T>

    If you want to use lambda expressions instead of the dynamic linq strings, you have to convert the List<DynamicNode> into List<dynamic>

    //Declare the Items as a DynamicNode list
    List<DynamicNode> children = @Model.Children.Items;

    //Declare the dynamicList type so the lambda knows that the delegate is a dynamic object
    List<dynamic> dynamicList = 
    children
    //Cast the list of DynamicNode objects to dynamic objects by passing in the convertion 
    //function into ConvertAll
    .ConvertAll<dynamic>(delegate(DynamicNode x) { return (dynamic)x; });

     //Use your lambdas! (how do you pluralize lambda?)
    dynamicList dynamicList.Where(x => x.location.cityid == sumNumber).ToList();

    If someone else has a better idea I'll look forward to it :)
     

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 14, 2011 @ 10:10
    Dan Diplo
    0

    Not sure if it will help in your case (as you didn't develop the custom DataType_, but you might want to read Gareth's blog-post on [I]RazorDataTypeModel - Datatype Developers Property Data / Model interface

  • Sean Tomlins 9 posts 29 karma points
    Oct 15, 2011 @ 12:15
    Sean Tomlins
    0

    I couldn't figure how to use the IRazorDataTypeModel with my cshtml script at all

Please Sign in or register to post replies

Write your reply to:

Draft