Compare Two Linq Queries - vb.net

I have the following two queries that take information from the same table
I need to compare the two tables and find all the information that's value is different. Is there any way to do this without having to make a loop?
Dim housepress = (From press In db.PressInfo
Where press.PressName = pressname And press.CustomerID = "House"
Select press).ToList()
Dim curpress = (From press In db.PressInfo
Where press.PressName = pressname And press.CustomerID = Customername
Select press)
I tried using curpress.Except but I get an error that "Local sequence cannot be used in Linq to SQL

Related

Using VBA Function in Access Query

I want to write an SQL query using MS access, which calculates a serial number using a VBA Function.
I have a big "samplebasicinformation" table which has lots of foreign keys and want to use the text fields in the foreign tables to create a text-based serial number.
I want the first field of the query to abbreviate a few text fields from other tables, then concatenate them and create this serial number.
In order to abbreviate the text fields I have the following function:
Function GetFirstLetters(rng As String)
Dim arr
Dim I As Long
arr = VBA.Split(rng, " ")
If IsArray(arr) Then
For I = LBound(arr) To UBound(arr)
GetFirstLetters = GetFirstLetters & Left(arr(I), 1)
Next I
Else
GetFirstLetters = Left(arr, 1)
End If
End Function
I've tried the below SQL to achieve this but failed due to syntax error.
SELECT
(getfirstletters(select sl.locationname from samplebasicinformation as sbi join samplelocation as sl
on sbi.samplelocationid = sl.samplelocationid)),
samplebasicinformationid
from samplebasicinformation as sbi1
Would anyone be able to offer some advice?
You need to feed a single column to your function.
Your query is rather confusing, I think it is as simple as this:
SELECT
getfirstletters(sl.locationname),
sbi.samplebasicinformationid
FROM samplebasicinformation as sbi
INNER JOIN samplelocation as sl
on sbi.samplelocationid = sl.samplelocationid
Note that you need INNER JOIN in Access Sql.

Conditionally remove rows from ms access query (SQL)

I would like to remove rows (about 10000) from a report in ms access. I am comparing two sets of two column in a table. When I run the query I would like it to remove the rows where column 1 and 3 match and column 2 and 4 match. I have tried running a left join query and trying with vba code but can't get it to work.
Here is the data
I have tried using VBA
Private Sub Report_Open()
Dim prism_box As String
Dim prism_recs As String
Dim keepdrop_box As String
Dim keepdrop_recs As String
prism_box = CStr(Me.[tbl_KeepDrop_remainingpackets_RecId])
prism_recs = CStr(Me.[tbl_KeepDrop_remainingpackets_RecId])
keepdrop_box = CStr(Me.[Duplicate Recids_Box#])
keepdrop_recs = CStr(Me.[Duplicate Recids_RecId])
If prism_box = keepdrop_box & prism_recs = keepdrop_recs Then
End If
End Sub
I have tried using query too but won't delete them.
I would like the final result to report a table like this...
Since this is a report, you are better off creating a query that excludes what you don't want to see.
Pull in whatever fields you need then create two more fields that have iif([column1] = [column3], True, False) and iif([column2] = [column4], True, False Add into criteria False and then point your report's recordsource at that query. (Change the column1-4 into their actual names)

Using SQL Wildcards with LINQ

I have a question about using LINQ to access a Database and trying to make use of it's version of accessing the LIKE comparison operator.
I know that LINQ has .Contains(), .StartsWith(), and .EndsWith() as comparison methods. However I am wondering if there is a way to programatically imcorporate SQL Wildcards into a LINQ statement without explicitly using these query operators. Let me explain my situation.
I am writing a program that accesses a database, and part of the program is a search window which the user can use to help them find specific database data. I would like to try and incorporate SQL Wildcards into the textbox fields for these search pages.
For example if a user enters the input 17% I'd want the program to check for anything in that specific column that starts with a 17. The same is true with %17 and 17 where I'd want it to search for columns that end with, and contain the values.
Currently, this is the code I have for my search method:
Public Function Data_Search(sData As List(Of String), DB As CustomDataContext) As DataGridView
Dim gridData As New DataGridView
Dim query = From p In DB.Parts
Select p.Part_Number, p.Part_Description, p.Supplier.Supplier_Name
for i = 0 To sData.Count - 1
If Not sData(i).ToString() = "" Then
Select Case i
Case 0
Dim partNum As String = sData(i).ToString()
query = query.Where(Function(x) x.Part_Number.Contains(partNum))
Case 1
Dim description As String = sData(i).ToString()
query = query.Where(Function(x) x.Part_Description.Contains(description))
Case 2
Dim supp As String = sData(i).ToString()
query = query.Where(Function(x) x.Supplier_Name.Contains(supp))
End Select
End If
Next
gridData.DataSource = query.ToList()
Return gridData
End Function
So right now I am trying to see if there is a way for me to modify the code in a way that doesn't essentially involve me putting a substring search into each Case section to determine if I should be using StartsWith(), Contains(), or EndsWith.
Thanks for your time.
If you are using LINQ to SQL, and you are talking to Microsoft SQL Server, then you can use SQLMethods.Like to implement SQL LIKE:
query = query.Where(Function(x) SQLMethods.Like(x.Part_Number, partNum))

Need to Pass multiple results from query back into one memo cell

I have been all over the web for days searching for a way to accomplish the following, and I am praying some experts can help me figure it out! I have a client that wants to dynamically build a shipping "approval" email by pulling various text statements in, based on the results of queries. The shipping details will be in one table, and the statements will be in another table. There could be multiple statements returned for each shipment (e.g. several training statements, several general statements). Each of these statements needs to be added into a memo cell (created to hold each type of statement) which will then be pulled into the email template for that shipment. The table structures are as follows.
Shipment_Table
*ID
*Shipment Type - Query Criteria
*Material Category - Query Criteria
*Permit Required - Query Criteria
*General Statement (memo field to hold all general statements that match criteria)
*Training Statement (memo field to hold all training statements that match criteria)
*Approval Statement (memo field to hold all approval statements that match criteria)
Statement_Table
*Statement Type (e.g. General, Training, Approval)
*Shipment Type - Query Criteria
*Material Category - Query Criteria
*Permit Required - Query Criteria
I successfully have a query (titled StatementSearch) that joins the two and pulls in the correct statements. I can't figure out how to take the multiple query results and append them into the memo cells for the shipment.
The code that has come close is below. It seems to correctly run my query and return the results, but is not putting the results into the memo field for my test record (ID =1 just to test the code, it will eventually run off of a form and the statements will generate off the click of a button).
I hope this isn't too confusing!
Option Explicit
Function StatementUpdate()
Dim dbs As DAO.Database
Dim rstStatements As DAO.Recordset
Dim rstCBG As DAO.Recordset
Set dbs = CurrentDb()
Set rstStatements = dbs.OpenRecordset("StatementSearch")
Set rstCBG = dbs.OpenRecordset("select [St_General]from
[Cross_Border_Grid_Table] where [ID]= 1")
rstCBG.MoveFirst
'loop through each record in the CBG that matches select query
Do Until rstCBG.EOF
rstStatements.MoveFirst
Do Until rstStatements.EOF
rstCBG.Edit
rstCBG![St_General] = rstStatements
rstCBG.Update
rstStatements.MoveNext
Loop
rstCBG.MoveNext
Loop
rstCBG.Close
rstStatements.Close
Set rstStatements = Nothing
Set rstCBG = Nothing
Set dbs = Nothing
Debug.Print "Done"
End Function
Try this for your loops
Dim concStatement as String
rstCBG.MoveFirst
Do Until rstCBG.EOF
concStatement = ""
rstStatements.MoveFirst
Do Until rstStatements.EOF
concStatement = concStatement & vbCrLF & rstStatements(0)
rstStatements.MoveNext
Loop
rstCBG.Edit
rstCBG![St_General] = concStatement
rstCBG.Update
rstCBG.MoveNext
Loop
I assume, your "StatementSearch" is a query that finds all statements according to ID=1. If there is more than one recordsets "rstCBG", you have to requery rstStatements in each loop step.
EDIT:
Changed rstStatement to rstStatement(0).
Also rstStatements(0) could be wrong: Change index = 0 to the index of the field in your query. As stated, the query is not visible in your question, so I do not know the right index number.

Distinct ComboBox Items

I am trying to convert sql to entity and I need to select distinct items. I thought this would work but its returning all the rows instead of the distinct items.
Dim OrderNos = (From r In Orders.R3Delivery Where r.mainOrderNumber <> "" Select r).Distinct().ToList()
For Each thisentry In OrderNos
cbOrderNumbers.DisplayMember = thisentry.mainOrderNumber
cbOrderNumbers.ValueMember = thisentry.mainOrderNumber
Next
Also is their any good free sql to linq tools out their linquer good but its like 60 quid
The problem is that the Distinct() is comparing the entire object being returned, not just the order number.
If you only need the order numbers, changing this line should get you there:
Dim OrderNos = (From r
In Orders.R3Delivery
Where r.mainOrderNumber <> ""
Select r.mainOrderNumber).Distinct().ToList()
If you need the whole object, then it gets more complicated.