Click here to Skip to main content
15,741,818 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
current im using a linq query to get the distinct values from the column now it arise a problem in it is it's not returing value in the variable (fldSuccesscount) which i mentioned below and it showing "
Object reference not set to an instance of an object."

Need to solve this Help me!!!
Thanks in Advance

below i atttached the table of the data
| card no. | status | output | op value | ip    |
| 12345    | 200    | jjksd  | 5        | dkfff |
| 34546    |        |        |          | jgkf  |
| 34124    | 404    | htdhh  | 5        | hdzg  |

What I have tried:

var fldSuccesscount = tmp_data.Where(p => p.fld_type.Contains("Get 
                       .Where(p => p.fld_status_code.Contains("200"))
                       .Where(p => p.fld_output_value.Contains("Success"))
                       .Select(a => new
Updated 30-Jan-23 22:04pm
Chris Copeland 31-Jan-23 4:38am    
I don't understand the comments you've been leaving, you're selecting values from the table where the field type must contain "Get Subscription", status code must contain 200 and the output must contain success. You said below "but distinct not work with not null values" which doesn't make sense, what exactly are you using Distinct() for, and what are you expecting to happen here?
Dave Kreskowiak 31-Jan-23 9:02am    
What do you think Distinct is for? Without a description of what you're trying to do with it, it's impossible to tell you what's going on and how your expectations may be wrong.

Think about it, can you use Contains method on a null string? The answer is no.

You have two choices:

1. Check for a null value:
var fldSuccessCount2 = tmp_data
    .Where(p => !string.IsNullOrWhiteSpace(p.fld_status_code) &&
    .Where(p => !string.IsNullOrWhiteSpace(p.fld_output_value) &&

2. use null coalescing[^] operator
var fldSuccessCount = tmp_data
    .Where(p => (p.fld_status_code?? "").Contains("200"))
    .Where(p => (p.fld_output_value?? "").Contains("Success"))

A couple of notes:
1. Select is not required for Count, only if you need to define a return type that is different to the original type in the list.
2. You do not need to use List as Distinct takes an IQueryable or IEnumberable type.
Share this answer
Manojkumar-dll 31-Jan-23 3:47am    
it works but the distinct is not perfectly working sorry that i have now updated the question and i have to distinct the list according with the fld_cust_smc
Graeme_Grant 31-Jan-23 5:32am    
I addressed the question regarding the empty or null exception. I also addressed other issues with your LINQ statement. I was not addressing the distinct, that was not part of your issues listed. Also, I don't have your code, nor can see your screen, so I have no way of knowing exactly what you are doing.
Try using the null conditional operator ?.
Member access operators and expressions | Microsoft Learn[^]
Share this answer
Graeme_Grant 31-Jan-23 1:56am    
Contains does not have an overload for null strings.
Manojkumar-dll 31-Jan-23 1:57am    
yes != works with it and gives me the not null values but distinct not work with not null values

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900