0

How to convert IPAddress to String is not that enough to use .ToString()? I do it in LINQ and I get :

Object reference not set to an instance of an object

here is my code:

 var x = (from t in db.v_vpn_gateway.AsEnumerable()

                 select new TurbineDvce
                 {
                     Comments = "VPN Gateway",
                     Description = string.Empty,
                     DeviceType = t.device_type,                                              
                     TurbineId = t.turbine_id.ToString(),
                     Username = string.Empty
                 })

TurbineId is string

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
moris62
  • 983
  • 1
  • 14
  • 41
  • Please, share a reproducible sample – Pavel Anikhouski May 21 '20 at 08:12
  • because t.turbine_id is null so you can't add .ToString() Do a null check, then add .ToString – mb14 May 21 '20 at 08:19
  • 2
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Selim Yildiz May 21 '20 at 08:22
  • BTW why do you need `AsEnumerable()`? – Guru Stron May 21 '20 at 08:23
  • @GuruStron in .Net Core i get an error stating multiple connection at the same time to database,then i found on the internet its better to apply the condition after converting to AsEnumerable – moris62 May 21 '20 at 08:26
  • @mortezasol there should be no problems with having multiple connection at the same time to postgres. Conditions i general should not by applied after `AsEnumerable` cause `AsEnumerable` will fetch whole table into memory. As for projection - it depends on how many column you actually need. – Guru Stron May 21 '20 at 08:33
  • @GuruStron thanks for the advice,but whats the reason its said,in .NetCore the query is done on the server side,so sometimes it has a problem with union and other conditions if you apply ,beuacse it cant transalte it on the server side,thats why it should be converted to AsEnumerable,whats your idea? – moris62 May 21 '20 at 08:40
  • @mortezasol it was not evident from you example that you was resolving such problems. Have you tried the null check? – Guru Stron May 21 '20 at 08:44
  • @GuruStron if i dont convert to AsEnumerable how should i check if its null or emtry,if its not null then convert it to string. – moris62 May 21 '20 at 08:46
  • @mortezasol you will need to check, but i would say you will not need to, if db can handle conversion from type of `turbine_id` to string(nvarchar) then it just will put nulls in your string. – Guru Stron May 21 '20 at 08:48
  • @GuruStron you are right,but i dont have a control over the DB otherwise i would ask them(other team) to do it – moris62 May 21 '20 at 08:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/214319/discussion-between-guru-stron-and-mortezasol). – Guru Stron May 21 '20 at 08:50

1 Answers1

0

Because t.turbine_id is null so you can't add .ToString() Do a null check, then add .ToString()

TurbineId = t.turbine_id ?? t.turbine_id.ToString()
mb14
  • 460
  • 4
  • 15