Use query syntax in GroupBy with complex range variable - vb.net

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.

Related

WHERE clause returning no results

I have the following query:
SELECT *
FROM public."Matches"
WHERE 'Matches.Id' = '24e81894-2f1e-4654-bf50-b75e584ed3eb'
I'm certain there is an existing match with this Id (tried it on other Ids as well), but it returns 0 rows. I'm new to querying with PgAdmin so it's probably just a simple error, but I've read the docs up and down and can't seem to find why this is returning nothing.
Single quotes are only used for strings in SQL. So 'Matches.Id' is a string constant and obviously not the same as '24e81894-2f1e-4654-bf50-b75e584ed3eb' thus the WHERE condition is always false (it's like writing where 1 = 0)
You need to use double quotes for identifiers, the same way you did in the FROM clause.
WHERE "Matches"."Id" = ...
In general the use of quoted identifiers is strongly discouraged.

How to write SSRS expression with OR condition involving a field and a parameter?

I have a set of tablix results that im trying to filter based on OR condition but im having difficulty combining the two into one custom expression. [IsSafetyObservation] is just a boolean field and [Department] needs to be part of the multi-selected parameter values of [#Department]. Here's a screenshot of what it looks like as two separate filters but this is doing AND condition. When I tried to write custom expression I'm not using the write syntax because I get a red line when I try to reference the #Department parameter
tablix filter screenshot
custom expression attempt
You'll need to combine your expressions into one as you tried but instead of using IN, you should use InStr. InStr searches for a string inside another string and returns the position or 0 if not found.
First you would need to combine your parameters into a string using JOIN.
JOIN(Parameters!Department.Value, ",")
This will combine your parameters into a single string that you can now search for the Department field in the Department parameter.
InStr(JOIN(Parameters!Department.Value, ","), Fields!Department.Value) > 0
Also, SSRS doesn't always work well with Booleans (ugh) - so an expression of = (1 = 1) may work in some places for a Boolean result but not others so it's best to specify:
=IIF(InStr(JOIN(Parameters!Department.Value, ","), Fields!Department.Value) > 0
OR Fields!IsSafetyObservation.Value = False,
True,
False)

How to compare dataframe to a string variable

I have a string variable dest which holds a certain value. I need to check if this variable exists on a registered temptable . I use the below query to find it.
terminatecheck = sqlContext.sql("""
SELECT 1 as op from known where node = """+dest +""" and 1=1
""")
Now i need to compare the value of terminatecheck to "1" and terminate a loop.
I checked and found that terminatecheck is a row object. How exactly do i compare this ?
if terminatecheck.op =="1":
does not work
Calling sqlContext.sql("Select...") will return a dataframe which is lazy evaluated. You need to call an action like first, take or collect to get the expression evaluated and the value returned. You probably want to call first which returns a single row (of type Row). take and collect returns an array (of type Array[Row]).
terminatecheck = sqlContext
.sql("""SELECT 1 as op from known where node = """+dest +""" and 1=1""")
.first

Linq Optional Where String

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

NHibernate RowCount in LINQ produces SQL Exception

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();