Issue related to IQueryable method (Cannot implicitly cast type 'long?' to 'long'

SuicideSheep

I have a IQueryable method as below:

public IQueryable<vmTest> TestMethod(long ID)
{
    return from m in db.table1
           join n in db.table2
           on m.table1_ID equals n.table1_ID into tabC
           from c in tabC
           join o in db.table3
           on m.table3_ID equals o.table3_ID
           where m.table1_ID.Equals(ID)
           select new vmTest{ field1 = m.xxx };
}

from the code above, m.xxx is a long type and in the vmTest there is a public long field1. There is error with m.xxx which saying Cannot implicitly convert type 'long?' to 'long'.An explicit conversion exists (are you missing a cast?).May I know what's wrong?

Additional information:
If i cast (long) in front of m.xxx, error will be dissapear but another problem occur when this query returning no value as it's wrong to cast long to null

Adriien M

You have several solutions:

  • You can change vmTest class definition and replace field1 type from long to long?
  • You can change your table in database and transforms xxx from NULL to NOT NULL then xxx will become a long
  • You can use a condition in your query like

    new vmTest { field1 = m.xxx.HasValue ? m.xxx.Value : 0 }
    

More information on Nullables here

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

C#, Cannot implicitly convert type `long' to `uint'

分類Dev

Cannot implicitly convert type error in generic method

分類Dev

HTC M8 (6.0.1): TreeSet<Long>.contains(Long) throws class cast exception: "cannot cast Double to Long"

分類Dev

How long* cast works

分類Dev

Long Array VBA Issue

分類Dev

Passing methods to Controller: java.lang.Long cannot be cast to java.lang.Integer

分類Dev

How to cast a value to long before aggregating it?

分類Dev

Cast double to unsigned long C++

分類Dev

Cannot implicitly convert type 'double' to 'int'

分類Dev

Cannot implicitly convert type 'Address' to 'String' in setter

分類Dev

Optimizing long running WebAPI method

分類Dev

JSON Loading in Java: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer

分類Dev

SDN4 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long when using AttributeConverter

分類Dev

Range of primitive type long in Java

分類Dev

rxjava timer subscribe NOT long type

分類Dev

PHP short date to long date issue

分類Dev

type unsigned long long int is it enough for 1 - 10^9 range?

分類Dev

Cannot parse long value to date in xslt

分類Dev

dynamic_cast issue with incomplete type

分類Dev

Call long running method and continue with other tasks

分類Dev

How to fix "class java.lang.Long cannot be cast to class java.lang.Integer" error in Java, when i want return result list

分類Dev

How to use Long data type in C?

分類Dev

Parameter type may not live long enough (with threads)

分類Dev

RuntimeError: expected scalar type Long but found Float

分類Dev

How to fix 'Cannot implicitly convert type' error in C#?

分類Dev

Cannot implicitly convert type object to string in PrivateObject in C#

分類Dev

cannot implicitly convert type 'int' to 'string' c#

分類Dev

webservice won't build - "Cannot implicitly convert type 'string' to 'int'"

分類Dev

Cannot implicitly convert type "CRMFullProject.Lab" to "SmartSecurity.Lab"

Related 関連記事

  1. 1

    C#, Cannot implicitly convert type `long' to `uint'

  2. 2

    Cannot implicitly convert type error in generic method

  3. 3

    HTC M8 (6.0.1): TreeSet<Long>.contains(Long) throws class cast exception: "cannot cast Double to Long"

  4. 4

    How long* cast works

  5. 5

    Long Array VBA Issue

  6. 6

    Passing methods to Controller: java.lang.Long cannot be cast to java.lang.Integer

  7. 7

    How to cast a value to long before aggregating it?

  8. 8

    Cast double to unsigned long C++

  9. 9

    Cannot implicitly convert type 'double' to 'int'

  10. 10

    Cannot implicitly convert type 'Address' to 'String' in setter

  11. 11

    Optimizing long running WebAPI method

  12. 12

    JSON Loading in Java: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer

  13. 13

    SDN4 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long when using AttributeConverter

  14. 14

    Range of primitive type long in Java

  15. 15

    rxjava timer subscribe NOT long type

  16. 16

    PHP short date to long date issue

  17. 17

    type unsigned long long int is it enough for 1 - 10^9 range?

  18. 18

    Cannot parse long value to date in xslt

  19. 19

    dynamic_cast issue with incomplete type

  20. 20

    Call long running method and continue with other tasks

  21. 21

    How to fix "class java.lang.Long cannot be cast to class java.lang.Integer" error in Java, when i want return result list

  22. 22

    How to use Long data type in C?

  23. 23

    Parameter type may not live long enough (with threads)

  24. 24

    RuntimeError: expected scalar type Long but found Float

  25. 25

    How to fix 'Cannot implicitly convert type' error in C#?

  26. 26

    Cannot implicitly convert type object to string in PrivateObject in C#

  27. 27

    cannot implicitly convert type 'int' to 'string' c#

  28. 28

    webservice won't build - "Cannot implicitly convert type 'string' to 'int'"

  29. 29

    Cannot implicitly convert type "CRMFullProject.Lab" to "SmartSecurity.Lab"

ホットタグ

アーカイブ