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

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

Related

Linq with multiple where clauses not pulling back data

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

How to speed up "inner join" query in access?

I'm working on an access project using vba and one of the programs has several queries like UPDATE A INNER JOIN B ON condition SET field = value .Both table A and Table B have hundreds of thousands records.
The annoying thing is these queries will take more than ten hours to execute and I was trying to use index to improve the performance but failed. Any suggestions to help me out?
Here is one of the queries:
strSQL = "UPDATE [csd_dis_" & i_year & "_all] as d INNER JOIN [adm_" & a_yyyy & "] as aa ON d.d_cino = aa.a_cino and cstr(d.d_sentdte) = cstr(aa.a_sentdte) and right('00' + trim(str(d.m_d_nature)),2) = left(trim(cstr(aa.m_a_nature)),2) "
strSQL = strSQL & "SET d.match_adm = " & kk & ",d.a_local_p = aa.a_local_p,d.inst = aa.c_inst, d.dosy = aa.dosy,d.dosm = aa.dosm,d.dosd = aa.dosd, " _
& "d.a_sentdte = aa.a_sentdte,d.tid = aa.tid,d.crb = aa.crb,d.crbchk = aa.crbchk, d.court = aa.court,d.ccn = aa.ccn,d.cyr = aa.cyr,d.a_nature = aa.nature,d.nature_o = aa.nature_o, " _
& "d.m_a_nature = aa.m_a_nature,d.class = aa.class,d.cat = aa.cat,d.ilossp = aa.ilossp,d.chap = aa.chap,d.sect = aa.sect,d.newoff = aa.newoff,d.hkid = aa.hkid,d.sex = aa.sex,d.doby = aa.doby,d.dobm = aa.dobm, " _
& "d.dobd = aa.dobd,d.a_dob = aa.a_dob,d.a_dob_imp = aa.a_dob_imp,d.age = aa.age,d.accom = aa.accom,d.disres = aa.disres,d.yrres = aa.yrres,d.edu = aa.edu,d.marst = aa.marst,d.bplbir = aa.bplbir,d.plbir = aa.plbir,d.yrhk = aa.yrhk, " _
& "d.[on] = aa.[on],d.sently = aa.sently,d.sentlm = aa.sentlm,d.sentld = aa.sentld,d.tr = aa.tr,d.dr = aa.dr,d.mr = aa.mr,d.inr = aa.inr,d.pvdg = aa.pvdg, d.pvndg = aa.pvndg,d.pvcn = aa.pvcn,d.pvcn2 = aa.pvcn2,d.bghome = aa.bghome, " _
& "d.pvdc = aa.pvdc,d.pvtc = aa.pvtc,d.pvdatc = aa.pvdatc,d.pvrc = aa.pvrc,d.pvpri = aa.pvpri,d.pvinst = aa.pvinst,d.foffen = aa.foffen,d.dfcy = aa.dfcy,d.dfcm = aa.dfcm,d.dfcd = aa.dfcd,d.a_dfcdte = aa.a_dfcdte, " _
& "d.agefcn = aa.agefcn,d.trisoc = aa.trisoc,d.ddpur = aa.ddpur,d.dcost = aa.dcost,d.cumd = aa.cumd,d.needle = aa.needle,d.yrdg = aa.yrdg,d.causa = aa.causa,d.fdg = aa.fdg,d.fmd = aa.fmd,d.agefdg = aa.agefdg, " _
& "d.yradd = aa.yradd,d.asso = aa.asso,d.la = aa.la,d.origin = aa.origin,d.mode = aa.mode,d.arrest = aa.arrest,d.stayy = aa.stayy,d.staym = aa.staym,d.stayd = aa.stayd,d.occ = aa.occ,d.name = aa.name,d.cname = aa.cname," _
& "d.othdocno = aa.othdocno,d.cudg1 = aa.cudg1,d.cudg2 = aa.cudg2,d.cudg3 = aa.cudg3,d.cudg4 = aa.cudg4,d.psydrug1 = aa.psydrug1,d.psydrug2 = aa.psydrug2,d.psydrug3 = aa.psydrug3, " _
& "d.psydrug4 = aa.psydrug4,d.agefpsy = aa.agefpsy,d.psydgfreq = aa.psydgfreq,d.psycausa = aa.psycausa WHERE '" & matchkey & "' and d.match_adm=' '"

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}