0
var data = mockDataDB.Data.AsQueryable()
   .Select(x => new ProductDto
   {
       Id = Convert.ToInt64(x.id), // this might fail because id might be null 
       Quantity = Int32.TryParse(x.quantity, out int somenumber) ? x.quantity : (int?)null
   }

Issue with this code is that x.id and x.quantity might be null sometimes, they are both of type string and id and quantity are type of int64 and int32..

How could I safely solve this?

Thanks

cheers

Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
  • @AhmedAbdelhameed strings which might be null, and in case when they are not null I want to convert them to integers.. – Roxy'Pro Oct 22 '19 at 13:53
  • Shouldn't that be `Int32.TryParse(x.quantity, out int somenumber) ? somenumber : (int?)null`? And how exactly does that not work? – juharr Oct 22 '19 at 13:53
  • 1
    `Id = int.TryParse(x.id, out int myID) ? myID : 0;` something along those lines should work. – Trevor Oct 22 '19 at 13:54
  • @juharr but what should I put on somenumber, why I can't use something more generic / dynamic? – Roxy'Pro Oct 22 '19 at 13:54
  • @Çöđěxěŕ what would be myID? – Roxy'Pro Oct 22 '19 at 13:55
  • 1
    @Roxy'Pro if the parse succeeds, it would be `x.id` otherwise it returns `0`... – Trevor Oct 22 '19 at 13:55
  • `myID` is the integer that the string `x.id` parsed to, if it parsed successfully. If `x.id` were `"56"` then `myID` would be `56`. – Joshua Robinson Oct 22 '19 at 13:56
  • If `Quantity` is a `int` and not a `int?` then you obviously cannot set it to `(int?)null`. You just have to decide what to set it to if the parsing fails. – juharr Oct 22 '19 at 13:57
  • Possible duplicate of [Better way to cast object to int](https://stackoverflow.com/questions/745172/better-way-to-cast-object-to-int) or https://stackoverflow.com/questions/4620565/int-tryparse-syntatic-sugar or https://stackoverflow.com/questions/1019793/how-can-i-convert-string-to-int – Trevor Oct 22 '19 at 13:59
  • Please post the class definition, I'm interested in knowing the types of `x.Id` and `x.Quantity` – Flater Oct 22 '19 at 14:08
  • @Flater They are types of Int? and long – Roxy'Pro Oct 22 '19 at 14:09
  • @Roxy'Pro: Then you are wrong about your claim that `x.Quantity` can be null, as `long` is not a nullable type – Flater Oct 22 '19 at 14:10

3 Answers3

1

You could inline a TryParse with a ternary expression, provided you have a default value in mind for Id when it's null.

var data = mockDataDB.Data.AsQueryable()
   .Select(x => new ProductDto
   {
       Id = Int64.TryParse(x.id, out long val) ? val : 0L, 
       Quantity = Int32.TryParse(x.quantity, out int somenumber) ? somenumber : (int?)null
   }
Jonathon Chase
  • 9,396
  • 21
  • 39
0

Something like this: Id = Int64.TryParse(x.id, out int somenumber) ? somenumber : 0

-1

Edited: You should check if the String variable (x.id) is null. If it is, you set it to zero or some default value that you would want. If its not, proceed with the converting. For example:

Id = (x.id == null) ? 0 : Convert.ToInt64(x.id);
ziga1337
  • 144
  • 1
  • 10