Linq with multiple where clauses not pulling back data - vb.net

I am using VB.NET and Linq; I have two separate conditions that have to be satisfied. When I run them individually they work fine however when I combine them into a single statement with multiple where statements then I get 0 records returned.
If I run the first where clause without the 2nd I get 70 records
If I run the 2nd where clause without the first i get 5 records
When I run with both where clauses I get 0 records returned
Here is the code.
submissionDetails = (From r In model.NIBRS_ReportStatusByORI
Where (sCritera.ORI.Contains(r.OriginatingORI) _
And Not r.ReportType = "ZERO REPORT" _
And r.OccurrenceDate >= sCritera.BeginDate _
And r.OccurrenceDate <= sCritera.EndDate _
And (r.ActionCode <> "D"))
Where (sCritera.ORI.Contains(r.OriginatingORI) _
And r.ReportType.Equals("ZERO REPORT") _
And r.YearMonthDate >= sCritera.BeginDate _
And r.YearMonthDate <= sCritera.EndDate _
And (r.ActionCode <> "D"))
Select New ReportStatusDetails() With {
.ChangeDate = r.Insertdate,
.IncidentId = r.IncidentID,
.IncidentIdentifier = r.IncidentIdentifier,
.OriginatingORI = r.OriginatingORI,
.ReportType = r.ReportType,
.StatusID = r.StatusID,
.SubmittedBy = r.Username,
.ReportDate = r.YearMonthDate,
.ReportDateString = r.YearMonth,
.OccurrenceDate = r.OccurrenceDate
}).ToList()

Have you tried this:
submissionDetails = (From r In model.NIBRS_ReportStatusByORI
Where (sCritera.ORI.Contains(r.OriginatingORI) _
And Not r.ReportType = "ZERO REPORT" _
And r.OccurrenceDate >= sCritera.BeginDate _
And r.OccurrenceDate <= sCritera.EndDate _
And (r.ActionCode <> "D"))
Select New ReportStatusDetails() With {
.ChangeDate = r.Insertdate,
.IncidentId = r.IncidentID,
.IncidentIdentifier = r.IncidentIdentifier,
.OriginatingORI = r.OriginatingORI,
.ReportType = r.ReportType,
.StatusID = r.StatusID,
.SubmittedBy = r.Username,
.ReportDate = r.YearMonthDate,
.ReportDateString = r.YearMonth,
.OccurrenceDate = r.OccurrenceDate
}).Union(From r In model.NIBRS_ReportStatusByORI
Where (sCritera.ORI.Contains(r.OriginatingORI) _
And r.ReportType.Equals("ZERO REPORT") _
And r.YearMonthDate >= sCritera.BeginDate _
And r.YearMonthDate <= sCritera.EndDate _
And (r.ActionCode <> "D"))
Select New ReportStatusDetails() With {
.ChangeDate = r.Insertdate,
.IncidentId = r.IncidentID,
.IncidentIdentifier = r.IncidentIdentifier,
.OriginatingORI = r.OriginatingORI,
.ReportType = r.ReportType,
.StatusID = r.StatusID,
.SubmittedBy = r.Username,
.ReportDate = r.YearMonthDate,
.ReportDateString = r.YearMonth,
.OccurrenceDate = r.OccurrenceDate
}).ToList()

Related

linq to sql vb.net case statements to add other test

It there a way to add other test to may request :
Dim exceptBanned = (From c In query _
Where Not bannedCCList.Contains(c.Field(Of String)("N°Reçu")) _
Group c By c!code_projet _
Into nb = Count(CInt(c("code_projet"))), ca = Sum(CDec(c("Prix") + CDec(c("tva")))) _
Select nb, ca).ToList
I want to add other test to my query , below what i have tried but unfortunately it doesn't work :
If tf = 1 Then
exceptBanned = exceptBanned.Where(Function(c) c!type.Equals("Transfert
acompte") = False)
else
exceptBanned = exceptBanned.Where(Function(c) c!type.Equals("Transfert
acompte") = True)
end if
Thank in advance
I have solved my issus ,and i'd like to share this solution here :
Dim qryResultsDes = (From order In GlobalDataDesistement.AsEnumerable() _
Where order!code_projet = tab.Rows(i).Item("code_projet"))
If tf = 1 Then
qryResultsDes = qryResultsDes .Where(Function(c) c!type.Equals("Transfert
acompte") = False)
else
qryResultsDes = qryResultsDes .Where(Function(c) c!type.Equals("Transfert
acompte") = True)
end if
Dim exceptBanned = (From c In qryResultsDes _
Where Not bannedCCList.Contains(c.Field(Of String)("N°Reçu")) _
Group c By c!code_projet _
Into nb = Count(CInt(c("code_projet"))), ca = Sum(CDec(c("Prix") + CDec(c("tva")))) _
Select nb, ca).ToList

How to transform an SQL statement with Group by and Order By into a LINQ statement in vb.net

I have the following code working correctly, but was requested to combine it into one LINQ statement:
Dim AddlOrders = From ords In ctxi.V_TKT_HIST_BVs.AsEnumerable() _
Select ords Where (ords.CUST_NO = cstno) And (ords.ORIG_STA_ID <> "SWWEB") _
Order By ords.ORIG_TKT_NO Descending, ords.TKT_DT Descending
Dim AddlOrds As New Collection(Of V_TKT_HIST_BV)
Dim o As New V_TKT_HIST_BV
If (cstno Is Nothing) OrElse (AddlOrders Is Nothing) OrElse (AddlOrders.Count = 0) Then
AddlOrdersLabel.Text = "You have 0 additional orders."
AddlOrdersGrid.Visible = False
Else
For Each ord In AddlOrders
If prevord = String.Empty Then
prevord = ord.ORIG_TKT_NO
totord = ord.TOT
o = ord
ElseIf prevord = ord.ORIG_TKT_NO Then
totord += ord.TOT
Else
o.TOT = totord
AddlOrds.Add(o)
prevord = ord.ORIG_TKT_NO
totord = ord.TOT
o = ord
End If
Next
If o IsNot Nothing Then
AddlOrds.Add(o)
End If
Dim Addord = From ords In AddlOrds Order By ords.TKT_DT Descending
AddlOrdersGrid.DataSource = Addord
I have tried the following statement, but Visual Studio changes "Into os" to "Into os()" and gives a message that Definition of method os is not accessible in this context:
Dim orders = From o1 In ctxi.V_TKT_HIST_BVs
Where o1.CUST_NO = cstno
Group o1 By o1.TKT_DT, o1.ORIG_TKT_NO, o1.TOT
Into os() Select ORIG_ORD_NO, total = os.Sum(TOT),
tdate = os.Last(Function(v) v.TKT_DAT)
An example of the SQL would be like:
SELECT TOP (200) CUST_NO, EMAIL_ADRS_1, SUM(TOT) AS Expr1, ORIG_TKT_NO,
MIN(DISTINCT TKT_DT) AS Expr2
FROM V_TKT_HIST_BV
GROUP BY CUST_NO, EMAIL_ADRS_1, ORIG_TKT_NO
HAVING (EMAIL_ADRS_1 LIKE 'name%')
ORDER BY Expr2
Does anyone have an idea why it would change os into a method?
This would be it in C#:
AddlOrderGrid.DataSource=ctxi.V_TKT_HIST_BVs
.Where(t=>t.CUST_NO==cstno)
.Where(t=>t.ORIG_STA_ID!="SWWEB")
.GroupBy(t=>t.ORIG_TKT_NO)
.Select(t=> new {
CUST_NO=cstno,
EMAIL_ADRS_1=t.FirstOrDefault().EMAIL_ADRS_1,
TOT=t.SUM(u=>u.TOT),
ORIG_TKT_NO=t.Key,
TKT_DT=t.Min(u=>u.TKT_DT)
})
.OrderByDescending(t=>t.TKT_DT);
Converted to VB.NET:
Dim Addord = ctxi.V_TKT_HIST_BVs _
.Where(Function(t) t.CUST_NO = cstno) _
.Where(Function(t) t.ORIG_STA_ID <> "SWWEB") _
.GroupBy(Function(t) t.ORIG_TKT_NO) _
.Select(Function(t) New With { _
Key .CUST_NO = cstno, _
Key .EMAIL_ADRS_1 = t.FirstOrDefault().EMAIL_ADRS_1, _
Key .TOT = t.SUM(Function(u) u.TOT), _
Key .ORIG_TKT_NO = t.Key, _
Key .TKT_DT = t.Min(Function(u) u.TKT_DT) _
}).OrderByDescending(Function(t) t.TKT_DT)
If (Addord.Any()) Then
AddlOrderGrid.DataSource=Addord
Else
AddlOrdersLabel.Text = "You have 0 additional orders."
AddlOrdersGrid.Visible = False
Endif

vb.net - using case select query in sql for jtable

If anyone knows how to use case in query for jtable, pls take a look my code.
Dim batch_status As String = " case when status = 0 then 'Created' when status = 1 then 'Scanning' when status = 2 then 'Scan Saved' end as status"
cmd.CommandText = "SELECT * FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY " & jtSorting & " ) AS RowNum, batch_id, batch_name, date_created, profile_id, total_page, " & batch_status & ", First_ScanID, file_id " & _
" FROM [ip_ent_site].[dbo].[tbl_batch] WHERE STATUS IN (0,1,2) ) AS RowConstrainedResult " & _
" WHERE RowNum >= #jtStartIndex AND RowNum < #jtEndIndex ORDER BY RowNum ; "
if i use like this, it is working:
Dim batch_status As String = "status"
but with case, not.
whats problem here?
another part of code:
If dt.Rows.Count > 0 Then
students = (From item In dt.AsEnumerable() Select New Class1 With { _
.No = Convert.ToInt32(item(1)), _
.batch_id = Convert.ToInt64(item(2)), _
.batch_name = DirectCast(item(3), String), _
.date_created = item(4).ToString, _
.profile_id = Convert.ToInt32(item(5)), _
.total_page = Convert.ToInt32(item(6)), _
.status = Convert.ToInt32(item(7)), _
.First_ScanID = Convert.ToInt32(item(8)), _
.file_id = CheckDBNullInteger(item(9)) _
}).ToList()
End If
Use your declare statement like below.
Dim batch_status As String = " case when status = 0 then ''Created'' when status = 1 then ''Scanning'' when status = 2 then ''Scan Saved'' end as status"
may be the problem with #jtStartIndex AND #jtEndIndex.
Just found, change .status = Convert.ToInt32(item(7)) to .status = item(7).ToString,
displaying in Jtable is quite different

Linq (Visual Basic) - Comparing two anonymous types yields no results

I am trying to write a linq query with a composite key. Here is the full query:
Dim qeRelationships = From p In qualificationElements _
Join r In relationships On New With {.ElementCode = r.Parent, .Type = r.ParentType} _
Equals New With {.ElementCode = p.ElementCode, .Type = p.Type} _
Join c In qualificationElements On New With {.ElementCode = r.Child, .Type = r.ChildType} _
Equals New With {.ElementCode = c.ElementCode, .Type = c.Type} _
Select New QualificationElementRelationship _
With {.Parent = p, _
.Child = c, _
.Relationship = r.RelationshipType, _
.Scope = r.Scope}
This is yielding no results and I am not sure why.
I have boiled it down to two examples. The following works and returns records:
Dim qeRelationships = From p In qualificationElements _
Join r In relationships On r.Child Equals p.ElementCode _
Select New _
With {.Relationship = r.RelationshipType, _
.Scope = r.Scope}
This next snippet does not work and does not return records
Dim qeRelationships = From p In qualificationElements _
Join r In relationships On New With {.a = r.Child} Equals New With {.a = p.ElementCode} _
Select New _
With {.Relationship = r.RelationshipType, _
.Scope = r.Scope}
Why does the first query work and the second fail?
Here is a quick post and answer! It is because all the properties of your anonymous class need to be marked up with the "Key" directive.
Dim qeRelationships = From p In qualificationElements _
Join r In relationships On New With {Key .a = r.Child} Equals New With {Key .a = p.ElementCode} _
Select New _
With {.Relationship = r.RelationshipType, _
.Scope = r.Scope}
The full query
Dim qeRelationships = From p In qualificationElements _
Join r In relationships On New With {Key .ElementCode = r.Parent, Key .Type = r.ParentType} _
Equals New With {Key .ElementCode = p.ElementCode, Key .Type = p.Type} _
Join c In qualificationElements On New With {Key .ElementCode = r.Child, Key .Type = r.ChildType} _
Equals New With {Key .ElementCode = c.ElementCode, Key .Type = c.Type} _
Select New QualificationElementRelationship _
With {.Parent = p, _
.Child = c, _
.Relationship = r.RelationshipType, _
.Scope = r.Scope}

list.longcount query using joins (Linq/Lambda)

I have two lists of class objects (Shifts and Employees) with which I am trying to create a join. I then need a count of the resulting items that match the search criteria.
Below is the code I am using, but because I can't find a way to pass the result back as a specific 'type' it's defaulting to boolean and warning me that this will cause a problem:
' Count Shifts for selected hour where: started before or on this
"hour" AND ends after or during this "hour" and Department = filter
value
intShift = Me.Shifts.LongCount(From myshift In Me.Shifts Join
myEmp In Me.EmployeesList On myshift.EmployeeName Equals myEmp.Name
Where myshift.Description = "Shift" And myshift.DateStart.Hour <=
myHour.Hour And myshift.DateEnd.Hour >= myHour.Hour _
And myshift.DateStart.Date = myDay.Date And myEmp.Department =
strFilter _ Or myshift.Description = "Overtime" And
myshift.DateStart.Hour <= myHour.Hour And myshift.DateEnd.Hour
>= myHour.Hour _
And myshift.DateStart.Date = myDay.Date And myEmp.Department =
strFilter)
The search without a join to the employee list and without the matching employee search filters works perfectly but just unable to combine the two.
I have searched for both linq/lambda and inner join examples, but I can't seem to find one that combines these with the longcount function.
I've tried to put together a minimal scenario based on your snippet.
If you request Count or LongCount directly from the query then the result appears to be as expected.
Dim Shifts() = {
New With {.DateStart = #1/1/2014#,
.DateEnd = #1/2/2014#,
.Description = "Overtime",
.EmployeeName = "Darren"}
}
Dim EmployeesList() = {
New With {.Name = "Darren",
.Department = "SO"}
}
Dim strFilter = "SO"
Dim myHour = #1/1/2014#
Dim myDay = myHour
Dim query =
From myshift In Shifts Join myEmp In EmployeesList _
On myshift.EmployeeName Equals myEmp.Name _
Where myshift.Description = "Shift" _
And myshift.DateStart.Hour <= myHour.Hour _
And myshift.DateEnd.Hour >= myHour.Hour _
And myshift.DateStart.Date = myDay.Date _
And myEmp.Department = strFilter _
Or myshift.Description = "Overtime" _
And myshift.DateStart.Hour <= myHour.Hour _
And myshift.DateEnd.Hour >= myHour.Hour _
And myshift.DateStart.Date = myDay.Date _
And myEmp.Department = strFilter
Console.WriteLine(query.Count)
Console.WriteLine(query.LongCount)