Check null value of a field result query Linq to Entity - vb.net

See the select below.
dim query = from a in MyContext.MyTables
Where Not a.ID = 1
Select a
I want to check if a value of a field is null. I am doing the following:
if query.count > o then
if a(0)("field") is nothing then
messagebox.show("INVALID VALUE FOR RETURNING")
end if
end if
It is generation an exepction that I can't compare the value a(0)("field").
How should I do it?
Thanks.

Related

iam trying to get datable specific columns which has null value using linq in vb.net get only those 2 empty rows

When I check the column values in a DataTable using LINQ, they are not equal. Here is the code I'm using which has the problem:
for eg if columnA 5 rows which two or empty ir null i should
DT.
Select("Source <> ('AMEX','VISA')").
ToList().
ForEach(Sub(row) row(ColumnExpType)="T")
To get all the rows, using LINQ, which have NULL in ColumnA you could do something like this:
DT.Rows().
Cast(Of DataRow).Where(Function(row) row("ColumnA") Is DBNull.Value)
However, depending on your needs, you may prefer to use the old Select method:
DT.Select("ColumnA IS NOT NULL")
If you also need to check if the column contains an empty string, then to do that with Select:
DT.Select("ColumnA IS NOT NULL OR ColumnA = ''")
But, if you want to use LINQ, then it depends on the type of the column and whether it's a typed DataTable (a table from a typed DataSet). If the column is a standard string type, like NVARCHAR in T-SQL, and the DataTable is just a standard non-typed DataTable, then you could do something like this:
DT.Rows().
Cast(Of DataRow).
Where(Function(row) row("ColumnA") Is DBNull.Value Or DirectCast(row("ColumnA"), String) = "")
Select("Source <> ('AMEX','VISA')") will not work.
You need
Select("Source <> 'AMEX' And Source <> 'VISA'")
Not sure what the For Each is doing. This is a working example of using my test database.
Dim lst = dt.Select("Rating <> 'Unrated' And Rating <>'Good'").ToList
For Each dr As DataRow In lst
Debug.Print(dr("Name").ToString)
Next

If Else using Column in Table

I have table named studentlogs, and it has column status on it. When I finished inserting records on student logs, I want to use an if .. else statement. The status column can only have 3 values: 1, 2 or 3.
Now, I want to do the following after I insert the records:
if status="1" then
CALL SENDSMS()
ENDif
if status="2" then
msgbox("")
ENDif
if status="3" then
msgbox("")
How can I do it when I am dealing with columns?
To get the value of a row in a specific column, you can use parenthesis right after the row object and specify the ColumnName or ColumnIndex property to get the value.
Also, when there is more than one potential value, you can ue a case statement, instead of a lot of ElseIf statements.
MVCE Setup
'declare local variables
Dim dr As DataRow
'create table
Dim studentLogs As New DataTable("StudentLogs")
'add columns
With studentLogs
.Columns.Add("Status", GetType(Integer))
.Columns.Add("OtherCol1", GetType(String))
End With
'add values
With studentLogs
dr = studentLogs.NewRow()
dr("Status") = 1
dr("OtherCol1") = "Joey"
studentLogs.Rows.Add(dr)
End With
Check Column Value / Row Status
For Each row As DataRow In studentLogs.Rows
Select Case row("Status")
Case 1
Call SENDSMS()
Case 2
MsgBox("2")
Exit For
Case 3
MsgBox("3")
End Select
Next

Vb.net - Handling null or no values returned from SQL Command object

we got this sql query:
Select EmployeeDesignation from Employee where EmployeeName = Paramater1
(Parameter1 is value is passed to this)
We are using this statement:
lEmployeeDesignation = _SQLCommand.ExecuteScalar()
3 scenario's:
Returns the employee Designation (record exists in table)
No value is set in database for Parameter1 - so should return NULL (record exists but has no value)
No employee record exists so returns nothing (record doesn't exists)
I'm struggling with 2 and 3 scenario - incase of Scenario 3 we want the application to fail, but struggling how to trap this error.
regards
lEmployeeDesignation = _SQLCommand.ExecuteScalar()
if lEmployeeDesignation IsNot Nothing AndAlso lEmployeeDesignation <> DBNull.Value then
' you have found your data....'
Else
if lEmployeeDesignation = DBNull.Value then
' you have a record for parameter1 but EmployeeDesignation field is null'
End If
End If
Notice the use of AndAlso to shortcircuit the evaluation process. If the first condition is false then the second one is not evaluated
You could eliminate the Null entirely by...
Select IsNull(Max(EmployeeDesignation),0) from Employee where EmployeeName = Paramater1
It's not a terribly good thing to do, but it works and as long as you're not doing too many of them it'll be fine.
You could also put in a Count(EmployeeDesignation)=0 to check scenario 3 though of course this couldn't be done on the same query or you would have to use a reader

LINQ to Entities .contains is ignoring results with NULL

I am new to the Entity Framework, and am struggling with what I hope is a basic problem. My code is here:
Dim accounts As List(Of STUDENT) =
(From a In SA.STUDENTs
Where (a.MATRIC_NO.Contains(matric) And a.FIRST_NAME.Contains(firstName) And a.MIDDLE_NAMES.Contains(middleName) And a.SURNAME.Contains(lastName) And a.PREFERRED_NAME.Contains(preferredName))
Select a).ToList
The query runs fine, until one of the search fields is NULL in the database. If, for instance, a matric number is entered in the seach interface but middle name is left blank, the query will not return any records if middle name is NULL in the database. If middle name is a blank space in the database then it will return the record.
Can anyone offer any pointers?
Many thanks!
You can add an extra check in your query. For example:
public function filterList(IEnumerable list, string name)
{
var filtered_list = list.Where(x=> x.Name.Contains(name) || string.IsNullorWhitespace(name)).ToList();
return filtered_list;
}
So you can see, if the name variable is empty, all elements will return true, so all elements will be return (no real filter applied).
So basically, you can change all filters from
something.Contains(anotherthing)
to
something.Contains(anotherthing) || string.IsnullOrWhitespace(anotherthing)
(From a In SA.STUDENTs
Where isnull(a.MATRIC_NO.Contains(matric) And a.FIRST_NAME.Contains(firstName) And a.MIDDLE_NAMES.Contains(middleName) And a.SURNAME.Contains(lastName) And a.PREFERRED_NAME.Contains(preferredName))
Select a).ToList
Like this `:select * from tbl where statusid = isnull(#statusid,statusid)
try like this ..
Dim get_rmf_2 = From rmf In t_rmf _
Where Not IsDBNull(rmf!NIVP) AndAlso rmf!NIVP = nivp_rap
this is in VB I think this is works fine
I managed to solve this using a different approach. If there was no value entered for a particular field, leave it out the query. I accomplished this using predicates, as below:
'create the base query
Dim accounts =
(From a In SA.STUDENTs
Select a)
'create predicates for each condition required in the query
If matric <> "" Then
accounts = accounts.Where(Function(m) m.MATRIC_NO.Contains(matric))
End If
If firstName <> "" Then
accounts = accounts.Where(Function(f) f.FIRST_NAME.Contains(firstName))
End If
If middleName <> "" Then
accounts = accounts.Where(Function(mn) mn.MIDDLE_NAMES.Contains(middleName))
End If
If lastName <> "" Then
accounts = accounts.Where(Function(l) l.SURNAME.Contains(lastName))
End If
If preferredName <> "" Then
accounts = accounts.Where(Function(p) p.PREFERRED_NAME.Contains(preferredName))
End If
'execute the query
Dim accountlist = accounts.ToList
'return the results
Return accountlist
If anyone can see anything wrong with this, or any gotchas that I'm unaware of, please let me know! I'm very new to LINQ to Entities, and LINQ in general!

LINQ To Entities .Any() results in null reference exception

I have a simple query as below:
Dim sizings = From a In db.Sizings
Where a.Customer.ID = customer.ID
Select a
If sizings.Any Then
.....
The sizings.Any line is throwing a null reference exception. I thought I was meant to use .Any to determine if there were any rows returned?
isnothing(sizings) returns false.
Any ideas?
Edit - Resolution:
Don't use null objects in the LINQ Query!
Try checking that Customer isn't null before comparing its ID.
Dim sizings = From a In db.Sizings
Where a.Customer IsNot Nothing And a.Customer.ID = customer.ID
Select a
If sizings.Any() Then
'
End If
What about using sizings.Count() > 0