Reputation: 575
I have a list like
var listDataDemo = model.Jobs.ToList();
The listDataDemo has data like following
Count = 13
[0]: {PaymentItemModel}
when i type in Immediate Window like
listData.FirstOrDefault()
it gives result
Amount: 0
Date: {1/01/0001 12:00:00 AM}
Method: "PayPal"
PayerName: null
PayerNumber: null
PaymentDetailType: Payment
TransactionId: null
But when i write(code in my class)
var demoVal = listData.FirstOrDefault(p=>p.Method=="PayPal")
The name 'demoVal' does not exist in the current context(no value)
How to get a value from LIST.
Please help me.
Upvotes: 0
Views: 3353
Reputation: 6175
Off the cuff, the predicate you're providing to FirstOrDefault to filter results looks like it has a syntax error: (p=>p.Method="PayPal")
should be (p=>p.Method=="PayPal")
.
Granted this was probably a typo, for completeness:
'=' is an assignment operator, for when you want to assign a value to a variable.
'==' is an equality comparison operator, for when you want to test equality between values and get 'true' or 'false'.
Edited to answer beyond the typo...
Are you using Entity Framework? EF has extension methods for FirstOrDefault also, except the filter parameter is an SQL query. If you're using EF, then you could be missing the following:
using System.Collections.Generic;
using System.Linq;
Upvotes: 1
Reputation: 3860
The below code is working fine for me. Just have a look:
List<test> testList = new List<test>();
testDB testObj = new testDB();
testList = testObj.fn_getAll();
var abc = testList.FirstOrDefault(a => a.Id == 3);
And you just try to change the name of the variable, this might be causing the issue.
I hope it will help you.. :)
Upvotes: 1
Reputation: 79441
It looks like the predicate in your call to FirstOrDefault
has a typo. You want ==
for comparison, not =
for assignment.
Upvotes: 0