Linq Entity compare 3 fields with wildcard vb.net - vb.net

Im sure this code was working but cannot rememeber what i did to make it work.
I want to match fields first middle and last with wildcard in between to string user
Return (From q In context.users
Where (Join( {q.user_first, q.user_middle, q.user_last}, "%"))
.ToLower.Contains(user.ToLower)
Select q).ToList
The error message is:
LINQ to Entities does not recognize the method 'System.String
Join(System.String[], System.String)' method, and this method cannot
be translated into a store expression.

Hope someone finds this useful.
I created a computed column in user table called user_fullname:
(rtrim((coalesce([user_first]+' ','')+coalesce([user_middle]+' ',''))+coalesce([user_last]+' ','')))
The code below, splits the name into an array to work out what to search for.
Dim names As String() = user.Trim.Split(" ")
If names.Count = 2 Then
Dim fname As String = names(0)
Dim lname As String = " " & names(1)
Return (From q In context.users Where (q.user_fullname.StartsWith(fname) And q.user_fullname.Contains(lname)) Select q).AsQueryable
ElseIf names.Count = 1 Then
Return (From q In context.users Where (q.user_fullname.StartsWith(user) Or q.user_last.StartsWith(user)) Select q).AsQueryable
ElseIf names.Count = 3 Then
Dim fname As String = names(0)
Dim lname As String = names(2)
Dim mname As String = names(1)
Return (From q In context.users Where (q.user_first.StartsWith(fname) And q.user_middle.StartsWith(mname) And q.user_last.StartsWith(lname)) Select q).AsQueryable
Else
Return Nothing

Related

Generate a code like "50VIABC001" and if the value "ABC" appear again than append "002", "003" and so on..?

I have a grid with many names when the user clicks on any name it binds the details of that name to textboxes, dropdown etc.,
In this I have a textbox which generates code for that name.
I'm generating a code something like this. First 4 digits "50VI" will remain same for all followed by 1 letter from each 'firstName', 'MiddleName' and 'LastName'. Finally, it should have 3 digit number starting from '001', '002' and so on.
This final 3 digit should be decided on the 3 letters from First middle and last names.
Example: 50VIFMS001 where F=FirstName M=MiddleName S=SirName..
If another person has similar name format then the code should be "50VIFMS002" and so on.
I have written the code in Vb.NET but now the value that I get is "50VIFMS000" where "000" is same everywhere. So I'm stuck with last 3 digit part.
Public Function GenNewCode()
Dim code As String = "50VI" & String.Concat(txtFName.Text.Trim,
txtMName.Text.Trim, txtLName.Text.Trim).ToUpper().Substring(0, 3)
Dim exec As New ExecuteQuery
Dim CNT As String = exec.ExecuteScalar("SELECT COUNT(*) FROM ADMDOCTMST WHERE DOCTCODE LIKE '" & code & "%'")
txtNewCode.Text = code & CNT.PadLeft(2, "0")
Return code
End Function
First, turn on Option Strict.
Public Function GenNewCode()
Functions have a return type.
Dim code As String = "50VI" & String.Concat(txtFName.Text.Trim, txtMName.Text.Trim, txtLName.Text.Trim).ToUpper().Substring(0, 3)
Check your parenthesis. The .ToUpper and .Substring are acting on the concatenated string; not each string individually so you are gettin the first three characters in the first name.
txtNewCode.Text = code & CNT.PadLeft(2, "0")
You are starting your sequence at 001, so if your query returns a count of 3. That would be 001, 002, 003. You need to increment the count to get a new value. Also, .PadLeft will only work up to 9. Check the length of the incremented value then pad accordingly.
Return code
You haven't changed the value of code since your original Dim code so you will not get the value you are expecting. Setting code & ... to a text box does not change the value of code.
Public Function GenNewCode() As String
Dim FName As String = "Mary"
Dim MName As String = "ruth"
Dim LName As String = "Smith"
Dim FInitial As String = FName.Substring(0, 1).ToUpper
Dim MInitial As String = MName.Substring(0, 1).ToUpper
Dim LInitial As String = LName.Substring(0, 1).ToUpper
Dim code As String = "50VI" & FInitial & MInitial & LInitial
Dim exec As New ExecuteQuery
Dim CNT As Integer = CInt(exec.ExecuteScalar("SELECT COUNT(*) FROM ADMDOCTMST WHERE DOCTCODE LIKE '" & code & "%'"))
CNT += 1
Dim cntStr As String = CStr(CNT)
Dim Padding As Integer = 3 - cntStr.Length 'You want a total length of 3 so subtract existing length from 3
code &= cntStr.PadLeft(Padding, CChar("0"))
Return code
End Function

Extracting last name from a range having suffixes using VBA

I have a list of full names in a column like for example:
Dave M. Butterworth
Dave M. Butterworth,II
H.F. Light jr
H.F. Light ,jr.
H.F. Light sr
Halle plumerey
The names are in a column. The initials are not limited to these only.
I want to extract the last name using a generic function. Is it possible?
Consider the following UDF:
Public Function LastName(sIn As String) As String
Dim Ka As Long, t As String
ary = Split(sIn, " ")
Ka = UBound(ary)
t = ary(Ka)
If t = "jr" Or t = ",jr" Or t = "sr" Or t = ",jr." Then
Ka = Ka - 1
End If
t = ary(Ka)
If InStr(1, t, ",") = 0 Then
LastName = t
Exit Function
End If
bry = Split(t, ",")
LastName = bry(LBound(bry))
End Function
NOTE:
You will have to expand this line:
If t = "jr" Or t = ",jr" Or t = "sr" Or t = ",jr." Then
to include all other initial sets you wish to exclude.You will also have to update this code to handle other exceptions as you find them !
Remove punctuation, split to an array and walk backwards until you find a string that does not match a lookup of ignorable monikers like "ii/jr/sr/dr".
You could also add a check to eliminate tokens based on their length.
Function LastName(name As String) As String
Dim parts() As String, i As Long
parts = Split(Trim$(Replace$(Replace$(name, ",", ""), ".", "")), " ")
For i = UBound(parts) To 0 Step -1
Select Case UCase$(parts(i))
Case "", "JR", "SR", "DR", "I", "II"
Case Else:
LastName = parts(i)
Exit Function
End Select
Next
End Function

mongodb query to find field - vb.net

How would I structure a query for mongodB to make a 'stored procedure' or make the request to select an id which is marked active and then delete that field immediately or mark it as inactive; whichever one has the better performance. Here is the collection structure:
db = server.GetDatabase("test")
siteCollection = db("test")
collection = db.GetCollection(Of BsonDocument)("siteids")
Dim book As BsonDocument = New BsonDocument() _
.Add("siteid", BsonValue.Create(BsonType.String)) _
.Add("active", BsonValue.Create(BsonType.String))
collection.Insert(book)
I found the java version and not sure if this will work and what is .net syntax
db.things.find( { x : 4 } , { j : 1 } )
This apparantly finds records where x = 4 but only return where j = 1 so I want one siteid where active = 'N'
Thanks; here is what I have come up with thus far:
' Dim squery = Query.EQ("active", "Y")
Dim squery = Query.EQ("active", "Y")
Dim ssort = SortBy.Null
Dim uupdate = Update.[Set]("active", "N")
Dim result = collection.FindAndModify(squery, ssort, uupdate)
' Dim dresult = collection.FindAs(Of BsonDocument)(squery)
Dim newSiteId As String = dresult.Count
As you can see the first line commented out I thought a simple select would be implemented but that comes back null. Then with the second last statement commented out that too returned value Null.

String conversion for SQL select statement

Using a SELECT statement I query a database and get back a result in the format: name1,name2,name3
Depending on the database entry this result could have any number of names: name1,name2...name(n)
I would like to use this data to query another database like so:
SELECT Name, SerialNo, Model FROM InstrumentTable where ID=1 and InstName IN name1,name2,name3
In order to do this I need to convert name1,name2,name3 to ('name1','name2','name3')
I have tried splitting the String into an array of Strings
Dim ref1s As String = cmdf1.ExecuteScalar()
Dim fields() As String = Nothing
fields = ref1s.Split(",")
and then concatenating them in an array
For i As Integer = 0 To fields.Count - 1
MsgBox(String.Concat("'", fields(i), "'"))
Next
but I haven't been able to figure out how to do it yet.
Adding the brackets at the start and end of the string shouldn't be a problem just adding the quotes to each name and separating them with a comma is the issue.
Can anyone help me with this?
You got a bit previous
For i As Integer = 0 To fields.Count - 1
fields[i] = String.Concat("'",fields[i],"'")
Next
Then
strValue = fields.Join(',')
user557425,
Something like this might point you in the right direction:
Dim lst As List(Of String)
For i As Integer = 0 to fields.Count - 1
if i = fields.Count - 1 Then
lst.Add(fields(i) & "'")
Else
lst.Add(fields(i) & "','")
End if
Next
Dim sqlSB As StringBuilder
sqlSB.Append("SELECT * FROM TABLE WHERE BLAH IN(")
For each s As String in lst
sqlSB.Append(s)
Next
'use the stringbuilder as command text in a SqlCommand...

Join two items from an array using VB

I am creating an array as follows
Dim strFriends(0 to 6) As String
strFriends(0) = "Bianca"
strFriends(1) = "Jeana"
strFriends(2) = "Sam"
strFriends(3) = "Jenna"
strFriends(4) = "Erin"
strFriends(5) = "Carolyn"
strFriends(6) = "Kate"
Dim myFriends As String
myFriends = Join(strFriends, ", ")
MsgBox myFriends
This will produce the following string: "Bianca, Jeana, Sam, Jenna, Erin, Carolyn, Kate"
But I need to retrieve specific items in array and display them as list, something like this:
e.g: If I want to select from strFriends, Kate ,Sam and Bianca
It should list like
Kate
Sam
Bianca
How to perform the task. I am really new to VB so I am confused with this simple task. Can anyone help.
Thank you
Dim strFriends(0 To 6) As String
strFriends(0) = "Bianca"
strFriends(1) = "Jeana"
strFriends(2) = "Sam"
strFriends(3) = "Jenna"
strFriends(4) = "Erin"
strFriends(5) = "Carolyn"
strFriends(6) = "Kate"
Dim objOutput As Text.StringBuilder = New Text.StringBuilder()
For Each strFriend As String In strFriends
Select Case strFriend
Case "Kate", "Sam", "Bianca"
objOutput.AppendLine(strFriend)
End Select
Next
MessageBox.Show(objOutput.ToString())
Or
For Each strFriend As String In strFriends
If MyLogicToDetermineSelected(strFriend) Then
objOutput.AppendLine(strFriend)
End If
Next
It's not clear why you need to pick items from the original array when you already know which items you want, so I won't answer that part of the question. As for displaying each item on it's own line, you can do this with String.Join:
Dim chosenFriends As String() = {"Kate", "Sam", "Bianca"}
Dim output As String = String.Join(Environment.NewLine, chosenFriends)