SQL IN STOR:
select * from Car_Company where (#id is null or ID=#id)
What is the code corresponding to the next line ( In Linq )
From A IN DB.Company Where (IIF(String.IsNullOrEmpty(TextBox1.Text),a.cNAME=TextBox1.Text)).ToList
I would recommend pulling your string evaluation out of the LINQ statement and evaluating it once then using the evaluated value in your query. Also, if the value is supposed to be a number, but you can't guarantee if the user supplied a number, you need to test it first to make sure that you have a number in order to avoid the casting issue. The LINQ query that you supplied doesn't quite make sense because the where clause returns a string not a true/false result. You need to compare the value with something in the data rows. I think you want something like:
Dim Id As Integer
If Integer.TryParse(TextBox1.Text, Id) Then
Dim Query = From A IN DB.Company Where A.Id = Id
End If
It's totally unclear... Try this...
(From A IN DB.Company Where IF(Not String.IsNullOrEmpty(TextBox1.Text),
A.cNAME=TextBox1.Text,
"Something else")).ToList
Related
I have a LINQ query, where I want to group elements from a collection. I'm trying to use query syntax to achieve my result, but I can't figure out how to describe a more complex range variable using query syntax.
Using method syntax, this works:
Dim R = someCollection.GroupBy(Function(x)
For Each cItem In x.Attributes
If cItem.Name = "N1" Then Return cItem.Value
Next
End Function)
I tried doing this in query syntax, but failed. So this doesn't work and gives an error: Range variable name can be inferred only from a simple or qualified name with no arguments.
Dim R = From q In someCollection
Group q By Function(x)
For Each cItem In q.Attributes
If cItem .Name = "N1" Then Return cItem .Value
Next
End Function
Into Group
I tried various other versions, but to no avail. How can I group by a complex parameter using a query syntax? Or should just stick to method syntax?
A For Each loop that exits the first time a condition is satisfied can generally be collapsed into a single call to First, FirstOrDefault, Single or SingleOrDefault. In this case, the best option depends on whether there will always be one match or there can be none or more than one.
Always one: Single
Never more than one: SingleOrDefault
Never less than one: First
None, one or many: FirstOrDefault
In this case, assuming the last case:
Group q By q.Attributes.FirstOrDefault(Function(a) a.Name = "N1")?.Value
Null propagation is used because the "OrDefault" means that Nothing may be returned.
Assume a sqlite database with an integer column.
Now, it tends to happen that the integer field contains NULL values (=unset) as well.
I would like to interpret NULL values as zero (0) when doing queries on that field.
Is there a way to tell sqlite that I like NULL handled like 0 in this case, especially when I do comparisons using a SELECT statement?
Since I construct the queries dynamically in C-like code, I like to be able to write something like this:
query = "SELECT * FROM tbl WHERE intField=" + IntToStr(anIntValue)
Currently, I work around this with code as follow, which I like to avoid:
if (anIntValue == 0) {
query = "SELECT * FROM tbl WHERE intField IS NULL OR intField=0"
} else {
query = "SELECT * FROM tbl WHERE intField=" + IntToStr(anIntValue)
}
Maybe there's an operator I can use in the query that converts NULLs to other values I specify?
Of course, another way would be to make sure one never ends up with NULL values in that field, but one might want to be able to tell when the value hasn't been set at all yet, so I like to be able to keep the NULL values in the database.
In Standard SQL this is called COALESCE:
COALESCE(col, 0)
Why using a proprietary extension like IFNULL/NVL if there's a Standard which is supported by every DBMS?
please, try ifnull function, see doc at http://www.sqlite.org/lang_corefunc.html#ifnull
You can use IFNULL
or
CASE WHEN fieldname IS NULL THEN 0 ELSE fieldname END
This works the same as isnull(fieldname, 0)
You can read more here
Although you could use null coalesce functionality of your RDBMS, a universal approach exists that lets you treat NULLs as zeros in a condition, like this:
String val = IntToStr(anIntValue)
"SELECT * FROM tbl WHERE intField=" + val + " OR (0="+ val+" AND intField IS NULL)"
I'm trying to pass some values from a vba function to an SQL.
This is my SQL
SELECT *
FROM Hierarchy3
WHERE ID IN (getList("1 and 2"));
this is the definition of the vba function:
Function getList(Measure As String) As String
when I call the function I get: 1,2,3 as a String.
if I run the SQL as
SELECT *
FROM Hierarchy3
WHERE ID IN (1,2,3);
it works fine, but combining the two doesn't work. So I guess the String type is wrong, can you please help?
Your problem is that your query does not work like you expect it. Your function returns a string, so the query performed is:
SELECT *
FROM Hierarchy3
WHERE ID IN ("1,2,3");
Notice the quotation marks. Basically you are comparing an integer to a string and so doesn't return any results.
What you can do is use the INSTR function to see if the ID can be found in the string:
SELECT *
FROM Hierarchy3
WHERE INSTR(getList("1 and 2"),ID);
Now it will work because ID 1 can be found in the string "1,2,3" so the INSTR function will return the position where it can be found.
Here is the scenario:
I have a SQL select statement that returns a binary data object as a string. This cannot be changed it is outside the area of what I can modify.
So for example it would return '1628258DB0DD2F4D9D6BC0BF91D78652'.
If I manually add a 0x in front of this string in a query I will retrieve the results I'm looking for so for example:
SELECT a, b FROM mytable WHERE uuid = 0x1628258DB0DD2F4D9D6BC0BF91D78652
My result set is correct.
However I need to find a Microsoft SQL Server 2008 compatible means to do this programatically. Simply concatenating 0x to the string variable does not work. Obvious, but I did try it.
Help please :)
Thank you
Mark
My understanding of your question is that you have a column uuid, which is binary.
You are trying to select rows with a particular value in uuid, but you are trying to use a string like so:
SELECT a, b FROM mytable WHERE uuid = '0x1628258DB0DD2F4D9D6BC0BF91D78652'
which does not work. If this is correct, you can use the CONVERT function with a style of 2 to have SQL Server treat the string as hex and not require a '0x' as the first characters:
SELECT a, b
FROM mytable
WHERE uuid = CONVERT(binary(16), '1628258DB0DD2F4D9D6BC0BF91D78652', 2)
I have a query where I am only interested in the row count, however the query that NHibernate produces does not work with Sybase. I already have a custom Sybase dialect, but I can't find where to override the rowcount.
Given the following code:
var a = from b in table where b.something = 5 select b
var rows = a.Count
Generates an SQL similar to this:
select cast(count(*) as INTEGER) as p1 from table
I don't get why NHibernate wants to cast the count result, nor how I can override the dialect or elsewhere so NHibernate doesn't include the cast. The result of a count is castable to integer anyways.
If I however use QueryOver, things work perfectly. The problem then however, is that one off my conditions is dependent on the length of a string (yes, the db design could be better, but I can currently not change it). Using linq to call .Length on a string in the conditions work. However I can't use the string length as a condition in the QueryOver expressions. I also need a contains operation, which works with linq, but not QueryOver.
Is there a way to override how the Count query is generated, so it will work?
I am only interested if there is any rows matching, not the count, is there a different way of doing that?
Can instead the QueryOver? interface to use the SQL length and in operators?
You can understand if there are any rows matching by using Any function like this:
var a = from b in table where b.something = 5 select b;
var isMatch = a.Any();