Compare Nullable Types In WHERE Clause Of Linq To Sql .NET?
May 4, 2012
I am trying to query for records where that column IS NULL:
Dim UnassignedSubSvc =
From c In CurrentContext.SubService
Where c.Product.ProductSubServiceId **is null**
Select c).ToList()
View 1 Replies
ADVERTISEMENT
Mar 1, 2012
I have the following NHibernate Linq query:
From eachLine In myNhSession(Of SamplePoco)()
Where eachLine.SampleIntField = 1234
The property SamplePoco.SampleIntField is type Nullable(Of Int32)
When I run the query, I get the following exception:
System.InvalidCastException: Unable to cast object of type 'NHibernate.Hql.Ast.HqlCoalesce' to type 'NHibernate.Hql.Ast.HqlBooleanExpression'
If I change the property type to Int32, it works. It seems that Nullable types are automatically converted into a coalesce expression by the Linq compiler.
Debugging the NHibernate, I just found out that this Where clause was converted into: {where ((eachLine.SampleIntField == 1234) ?? False)}. As I can understand, the whole condition comparison was translated to be coalesced instead of just the Nullable property.
If I put this way eachLine.SampleIntField.Equals(1234) it doesn't work as well ('Equals not implemented' exception)
If I change the query to the following code, it works:
From eachLine In myNhSession(Of SamplePoco)()
Where {1234}.Contains(eachLine.SampleIntField)
Another code that works as well (coalescing the field properly as I was expecting by the first query):
From eachLine In myNhSession(Of SamplePoco)()
Where If(eachLine.SampleIntField,0) = 1234
View 1 Replies
Aug 27, 2010
I have: Dim nVar1 As Long?Dim nVar2 As Long?Dim nVarSum As Long?nVar1 = Nothing nVar2 = 5 nVarSum = nVar1 + nVar2 I would prefer the result to end with nVarSum being 5, instead of Nothing. I understand if you add something to an unknown value, you will end up with "somthing + unknown" or x+5 will always equal "x+5" not "5" because you are still carrying around that unknown "x". However, how can I effectively treat an unknown or Nothing as a zero for the purposes of addition in this case?(What is basically happening is that the end user is sending us a data file, this code parses that file and then sums together about 15 fields. If the user leaves those fields blank instead of assigning a zero to them, I need to treat it as if it was a zero for this one addition operation, but all the rest of the code needs to continue seeing it as a Nothing value since the user did not ACTUALLY submit zero... they submitted blank or nothing)
View 3 Replies
Jun 27, 2012
Can Nullable Types be used in VB.NET? If so, is it possible to have a Nullable Integer that I can use with a field that accepts NULL in SQL Server?
View 4 Replies
Aug 25, 2010
I'm converting an Access VBA app to VB.NET. It has dates defined as variants or objects to handle null values. I thought this would be a great chance to use the Nullable type. But I can't get it to work. Either it is inappropriate for what I am trying to use it for, or I am doing it wrong. Anyone know how to use it? Reader is a SqlDataReader.
[Code]...
I was hoping I didn't have to put a lot of If .. Null statements throughtout the code, but that is my only option unless someone has any other idea.
View 11 Replies
Feb 15, 2011
C#'s 'as' keyword will let you do this: int? input = value as int? Here's what I would assume the vb.net equivalent would be: Dim input As Integer? = TryCast(value, Integer?) There's an intellisense error in the TryCast stating the operand must be a reference type but Integer? is a value type.
Intellisense on Nullable(Of Integer) says 'Represents an object whose underlying type is a value type that can also be assigned null like a reference type.' It seems C#'s 'as' handles this like a reference type where TryCast doesn't have this built in. In VB10 I was able to take advantage of the new CTypeDynamic function to do the casting. Conversion.CTypeDynamic Method
Dim input As Integer? = CTypeDynamic(Of Integer?)(value)
Or:
Dim input As Integer? = CTypeDynamic(Value, GetType(Integer?))
There's a cost here as CTypeDynamic examines the type at runtime.
My question is what is the elegant way to handle this without CTypeDynamic?
View 6 Replies
Nov 15, 2011
Dim input As Integer? = TryCast(value, Integer?)There's an intellisense error in the TryCast stating the operand must be a reference type but Integer? is a value type.Intellisense on Nullable(Of Integer) says 'Represents an object whose underlying type is a value type that can also be assigned null like a reference type.'It seems C#'s 'as' handles this like a reference type where TryCast doesn't have this built in.
View 1 Replies
Aug 24, 2011
I have recently been working on a number of sections of code that deal with the insertion of Nullable types into a database. As I'm sure anyone who has dealt with similar code will be aware of the annoyance of constantly writing conditional logic to deal with the insertion of nulls into a database. I.e.:
MyValue.HasValue ? MyValue.Value : DBNull.Value;
If(MyValue.HasValue, MyValue.Value, DBNull.Value)
Basically I am just wondering why DBNull.Value exists and why Null simply couldn't be used?
View 1 Replies
Jan 17, 2010
It seems like overkill to set the value of a nullable type and implement iNotifyPropertyChanged. Is there a better way of doing this?
[Code]...
View 2 Replies
Jun 9, 2011
I'm trying to compare two variables of type nullable(of boolean) in VB.NET 2010. One of the variables has a value False and the other is Nothing. Now I was expecting the following expression to evaluate to true, but this is not the case:
Dim var1 as nullable(of boolean) = False
Dim var2 as nullable(of boolean)
var2 = Nothing
[Code].....
Why don't I see my MsgBox? How should I compare two nullables (of boolean)?
View 4 Replies
Jun 23, 2012
I need to compare two dictionary values if the types stored are equal, this is what i have
if gettype(Args(key)) = gettype(argtypes(key)) then
'' do something
end if
[Code]....
View 1 Replies
Mar 9, 2010
Is there any difference between the 2 methods below for calculating c ... specifically boxing/unboxing issues?
Dim a As Integer? = 10
Dim b As Integer? = Nothing
Dim c As Integer
' Method 1
[code]....
View 3 Replies
Jan 9, 2011
I have a class that implements a range of numbers....call it NumericRange(Of T). Internally, NumericRange stores T as a Nullable, T?. I have another class that wraps this class as NumericRange(Of UInt16). Call this class MyNumRange (I'm being simplistic here). So In MyNumRange, I have a few constructors defined:
[Code]....
View 2 Replies
Dec 8, 2011
I'm trying to do something similar to what's described here, but with nullable types.
[URL]
int availableUnits = unitsInStock ?? 0;
In VB, it would be this:
Dim availableUnits As Int32 = If(unitsInStock, 0)
However I'm working with db columns, which could be DbNull, and nullable types, which can be Nothing (which is different to DbNull). If a column is DbNull, I want to return Nothing, otherwise return the value. For eg:
Dim availableUnits As Int32? = If(myDataReader("UnitsInStock").Value, Nothing)
The error I'm getting is "Specified cast is not valid" but I'm not sure why. I've tried this as well:
Dim availableUnits As Int32? = If(isDbNull(myDataReader("UnitsInStock").Value), myDataReader("UnitsInStock").Value, Nothing)
Which is messy and just results in the same error. The only thing that works is this:
Dim availableUnits As Int32?
If isDbNull(myDataReader("UnitsInStock").Value) Then
availableUnits = myDataReader("UnitsInStock").Value
Else
availableUnits = Nothing
End If
Which is just silly. Is there a better way of getting nullable db values into nullable variables that I'm not aware of?
View 3 Replies
Jul 21, 2011
I'm using my data objects in a crystal report. The thing is, when I select my object, it gets added to the field explorer table. My properties are listed except for the Nullable Type properties. Why would this happen. I've had quite a few issues yesterday as this is my first attempt at a crystal report in .Net. I thought I worked around them but now I've hit this snag.
View 11 Replies
Apr 21, 2010
i have a simple code just have a look End Class
[Code]...
When i add num1 and num2 as above, compiler gave error that -- Operator + is not defined for types "type" and "type".How to define the meaning of '+' or any other operator in such situations?
View 11 Replies
Jan 10, 2012
Is there a technical reason why there is no implicit conversion from DBNull to the various nullable and/or sql types? I understand why the conversions don't currently happen, but don't understand why an implicit conversion wasn't created at the time or added in subsequent versions of the framework.Just to be clear, I'm looking for technical reasons, not "because that's they way they did it" or "I like it that way".
View 2 Replies
Jul 6, 2011
I have written an application which manages some bookings, and want to use Crystal Reports to provide the reporting engine. I have used it before with datasets with no problems.
I am using EF4, and have a LINQ query and report datasource assignment as below:
Code:
Dim ActiveCustomers = From Customer In FMEntities.Customers
Where Customer.Status = 1
Select Customer
[Code].....
I have read up that I may need to convert the incoming data, but I am confused as to what it needs to be converted to. Should it be a datatable?
View 1 Replies
Oct 6, 2011
I get the error
Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
in the following code..
Public Sub selCurrentManuf_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles selCurrentManuf.SelectedIndexChanged
End Sub
The drop list to go with it is the following...
<asp:DropDownList
OnSelectedIndexChanged="selCurrentManuf_SelectedIndexChanged"
selectedvalue='<%#Container.DataItem("c1_manufidcurrent")%>'
[code]....
View 1 Replies
Aug 23, 2009
I created a lable to handle DateTime = Now, It works fine. but when I moved it inside a FormView I Get the following Message. Handles clause requires a WithEvents variable defined in the containing type or one of its base types?
View 5 Replies
Oct 19, 2010
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Click, Open.Click,Name.Click
SelectVid.ShowDialog()
End Sub
[code]....
I keep getting this error:
Error1'.' expected.
Error2Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
Error 2 is also error 3
View 2 Replies
Dec 8, 2011
I am auto generating data grid and I am using check box in Data grid View,Now i am invoking check box state changed event but it produces the following error Handles clause requires a With Events variable defined in the containing type or one of its base types.
Here the code
Private Sub PRNT_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PRNT.CheckedChanged
Some Stuff.
End Sub
View 2 Replies
Mar 25, 2010
I am in the processing of moving an app from VB to C# but some of the classes I will just be moving to VB dlls to access from the main C# app. In doing this I am trying to get the VB dlls to utilize the main C# class lib. In this C# lib I have an abstract clase called base process, it sets up a background worker and handles all the progress tracking, errors, and even reports an ETA. To use this in a derived class in C# you would just do this...
public class SomeLongProcess : Common.Multithreading.BaseProcess
{
public void start()
[code]....
The problem is that if I do the equivelant in VB I get an error "Handles clause requires a WithEvents variable defined in the containing type of one of its base types". Well I can't do a WithEvents in the base class because it is C# so..
View 3 Replies
Aug 26, 2011
I am trying to follow this thread
[URL]
and I have it pretty close but have one error.
Quote:Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
That error is on both of these, in blue
Private Sub SpellChecker1_DeletedWord(ByVal sender As Object, ByVal e As NetSpell.SpellChecker.SpellingEventArgs) Handles SpellChecker1.DeletedWord
'save existing selecting
[code]....
I have added the reference, I have also added them to the toolbox. My dictionary is all set. What did I miss?
View 1 Replies
Nov 22, 2011
i am getting errors on last two event handlers. what did i do wrong?
Error - Handles clause requires a WithEvents variable defined in the containing type or one of its base types.D:Shipping ApplicationShipping ApplicationForm1.vb75136Shipping Application
Error - 'PrintDocument1' is not declared. It may be inaccessible due to its protection level.D:Shipping ApplicationShipping ApplicationForm1.vb1119Shipping Application
[code]....
View 2 Replies
Apr 23, 2009
Pretty new to VB.Net. Having a bit of trouble here with something I though should be simple.Keeping it simple. Let's say i have a Document table with "Name" that I want to search on (in reality there are several other tables, joins, etc ..). I need to be able to build the query where clause based on string values passed in.
Example - user may pass in "ABC" or "ABC DEF" or "ABC DEF GHI".The final query would be (syntax not correct i know)Select * from Documents Where Name Like %ABC% AND Name Like %DEF% AND Name like %GHI%
So, I though I could do something like this.
Dim query = From document In _context.Documents
<< loop based on number of strings passed in >>
query = query.Where( ... what goes here?? )
For some reason, being brain dead or something, I can't figure out how to make this work in VB.Net, or if I'm doing it correctly.
View 5 Replies
Nov 25, 2009
Dim dataContext As New ACSLostFoundEntities()
Dim query = From s In dataContext.FosterForms() _
Join c In dataContext.Fosters() _
On c.License Equals s.License _[code]....
At the end of this I want to do a Where WantedPets.Contains(criteria), is this possible?
View 1 Replies
May 11, 2009
Just upgraded a VS 2005 ASP.NET 2.0 website to VS 2008 ASP.NET 3.5. There was an error on the Sub Button_Click. It seemed to be a minor error, the website and the button worked just fine, as usual. What does the error mean?
Protected Sub btnDEreports_Click(ByVal
sender As
[code]...
Error 20 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
View 3 Replies
Jun 28, 2011
I have the following linq query:
Dim q = From definition In definitionList _
Where definition.ValueDefName.Contains(criteria)[code]....
I have already looked into this answer: LINQ - dynamic WHERE clause?Unfortunately, it doesn't work for me using .net 4.0. When I attempt to pass the criteria string in it ask for a predicate. The end goal is to add any of these:
definition.ValueDefname.Contains(criteria)
definition.ValueDefDesc.Contains(criteria)
definition.Aliaslist.Contains(Criteria)
definition.StrategicInitiative.Contains(Criteria)
to be passed into the query depending on what checkboxes the user has selected. How can I create a dynamic where clause in linq to sql? Is there new syntax for passing in a where clause as a string?
View 2 Replies
Apr 13, 2012
I'm need to do the equivalent of a t-sql statement like this using LINQ:
SELECT * FROM mytable WHERE idnumber IN('1', '2', '3')
so in LINQ I have:
Dim _Values as String = "1, 2, 3"
Dim _Query = (From m In mytable Where idnumber = _Values Select m).ToList()
Not sure what to do with _Values to make idnumber evaluate each value in the string.
View 2 Replies