none
Dynamic Linq : Methods on type 'Decimal?' is not accessible RRS feed

  • Question

  • Hi,

    I am using dynamic linq library to create dynamic linq queries in my silverlight client. I downloaded this library code (Dynamic.cs) from microsoft website after reading Scott page.

    I am using Linq to Entities to query from Oracle database. These dynamic queries work fine for string (VARCHAR in oracle db) but not working for decimal (NUMBER(10) in oracle db). It throws exception saying "Methods on type 'Decimal?' is not accessible ".

    Any body has any help on this please?

     

    Wednesday, December 28, 2011 11:08 AM

Answers

  • Hi Javed937,

    Could you please post your code here? I have tested on my computer, it works well. Below is the Model, code and the result.

    Model:

    The Name is a string type, Number is a int type, NumberCanNull is a int type which is nullable, DecimalNotNull is a decimal type, DecimalCanNull is a decimal type which is nullable.

    Code:

    class Program
        {
            static void Main(string[] args)
            {
                TestContainer context = new TestContainer();
                object [] par = new object[]{"Test",1,1,1,1};
                var query = context.TestEntities.Where("Name==@0 and Number==@1 and NumberCanNull == @2 and DecimalNotNull==@3 and DecimalCanNull==@4", par).Single();
                Console.WriteLine(query.Name);
                Console.WriteLine(query.Number);
                Console.WriteLine(query.NumberCanNull);
                Console.WriteLine(query.DecimalNotNull);
                Console.WriteLine(query.DecimalCanNull);
                Console.Read();
            }
        }
    

    The result:

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Javed937 Tuesday, January 3, 2012 5:52 AM
    Monday, January 2, 2012 8:41 AM
    Moderator

All replies

  • Hi Javed937,

    Welcome to MSDN Forum.

    Based on the issue, because the provider is the 3rd party, so I have no environment to reproduce the issue. I suggest you to repost it in Oracle Forum. This is, so you may get more help.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Friday, December 30, 2011 3:58 AM
    Moderator
  • Hi Allen,

    The issue can be reproduced with any framework entity or even with a plane Decimal? array. The problem is, dynamic query is not created for decimal? data field/property.


    Senior Software Developer
    Friday, December 30, 2011 6:11 AM
  • Hi javed937,

    Is the decimal data type nullable?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 2, 2012 4:02 AM
    Moderator
  • Yes,
    Senior Software Developer
    Monday, January 2, 2012 6:30 AM
  • Hi Javed937,

    Could you please post your code here? I have tested on my computer, it works well. Below is the Model, code and the result.

    Model:

    The Name is a string type, Number is a int type, NumberCanNull is a int type which is nullable, DecimalNotNull is a decimal type, DecimalCanNull is a decimal type which is nullable.

    Code:

    class Program
        {
            static void Main(string[] args)
            {
                TestContainer context = new TestContainer();
                object [] par = new object[]{"Test",1,1,1,1};
                var query = context.TestEntities.Where("Name==@0 and Number==@1 and NumberCanNull == @2 and DecimalNotNull==@3 and DecimalCanNull==@4", par).Single();
                Console.WriteLine(query.Name);
                Console.WriteLine(query.Number);
                Console.WriteLine(query.NumberCanNull);
                Console.WriteLine(query.DecimalNotNull);
                Console.WriteLine(query.DecimalCanNull);
                Console.Read();
            }
        }
    

    The result:

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Javed937 Tuesday, January 3, 2012 5:52 AM
    Monday, January 2, 2012 8:41 AM
    Moderator
  • I want to use LINQ functions like Equals(), Contains() etc. instead of "==" operator.

    Can you please check with this?

    And I am assuming "DecimalCanNull" looks like

    decimal? DecimalCanNull;


     


    Senior Software Developer
    • Edited by Javed937 Monday, January 2, 2012 11:26 AM
    Monday, January 2, 2012 11:24 AM
  • Hi,

    look at this EQUAL() in LinQ

     string word = "File";
          string[] others = { word.ToLower(), word, word.ToUpper(), "fıle" };
          foreach (string other in others)
          {
             if (word.Equals(other)) 
                Console.WriteLine("{0} = {1}", word, other);
             else
                Console.WriteLine("{0} {1} {2}", word, '\u2260', other);
          }      

    PS.Shakeer Hussain
    Monday, January 2, 2012 11:36 AM
  • Hi Javed937,

    I modified 'Name==@0' to 'Name.Equals(@0)', it still works well. The Dynamic Linq Library doesn't override Equals method for value type, so you can use it to int and decimal.

    Yes, the DecimalCanNull is decimal? DecimalCanNull.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us



    Tuesday, January 3, 2012 2:09 AM
    Moderator
  • Hi Allen,

    So you mean to say Equals() will work for int and decimal but not for int? and decimal?

    In my case "Name.Equals(@0)" also work but "DecimalCanNull.Equals(@0)" will not work. In dynamic.cs, It fails at the point

    static bool IsPredefinedType(Type type)
            {
                foreach (Type t in predefinedTypes) if (t == type) return true;
                return false;
            }

    Here type of decimal? does not match with the type of decimal and hence this fails and throws the above exception.


    Senior Software Developer
    Tuesday, January 3, 2012 3:22 AM
  • Hi Javed937,

    No, Equals method will not works for both 'decimal' and 'decimal?'.

    The default implementation of Equals supports reference equality only, but derived classes can override this method to support value equality.

    In this Dynamic Linq Library, there's no overriden method of the Equals for value types, 'decimal?' type is also a value type, except that it can be null.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 3, 2012 3:30 AM
    Moderator
  • Ok,

    So it means I can only use ==, != and other overridden operators only for value types. I cannot use functions like Any() etc. Thats reallly annoying. Standard LINQ supports all of them.


    Senior Software Developer
    Tuesday, January 3, 2012 3:36 AM
  • Hi Javed937,

    Please look at the API you have downloaded, which is in your LinqSamples\DynamicQuery\ folder, at the 'operators' section, it said 'A subset of the Standard Query Operators is supported for objects that implement IEnumerable<T>. ' Any() is one of them. Obviously, value type is not eligible for that.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 3, 2012 3:47 AM
    Moderator