I have two fields in my dataset [FirstName] and [LastName]. I get an System.Data.EnumerableRowCollection Object with the following query:
Dim query = From dt In _dataset.DataTable1
Where TeamName = "The Avangers"
Select dt.FirstName , dt.LastName
I am looking for a way to merge the FirstName and LastName to get a StringArray with the following entries:
Item(0): "Banner, Bruce"
Item(1): "Stark, Tony"
Item(2): "Rogers, Steve"
...
What I did is I performed a separate query for First and LastName so I could use .ToArray on both queries. Afterwards I merged them in a for each loop. Just wanted to ask if there is a faster way to perform that.
Just concatenate your FirstName and LastName in your query and then apply ToArray at the end.
Select dt.LastName + ", " & dt.FirstName
Related
I am working on a C# desktop application. I want to create a search functionality. Now the problem is that i am using around 8 textboxes. Different permutations of textboxes could be populated and the resulting 'sql where' condition should only include those textboxes values which are not null. Now one pathetic way is to use a zillion 'if and else' which obviously is laborious. Any other way to do this?
You need just one query with filled WHERE to use all parameters like this
select ...
from ...
WHERE
(firstNameColumn=:firstNameParam or :firstNameParam is null)
AND (lastNameColumn=:lastNameParam or :lastNameParam is null)
AND (...)
I would like to make a point of first checking is the paramtere null, then use it to compare with column values.
Since you are generating query in C#, try old-Chinese approach from Ming period of using default condition where 1=1 just to avoid checking did you already had first condition :)
string query = "select ... from ... join ... on ... where 1=1";
//suposedly you have value of one search box in variable called "item_name"
if(string.IsNullOrWhiteSpace(item_name) == false)
{
query += " and Order_Line.Name ='" + item_name + "'";
}
and so on for other fields.
What you are trying to do in order to avoid ifs is not really a good approach. Look at this:
string query = " select ... where Order_Line.Name = '" + item_name + "'";
What will be the resulting string if item_name is actually null?
EDIT: the resulting query would be
where Order_Line.Name = '' or Order_Line.Name is null
which is not what you want. You want every row if that search field is empty, menaing it shouldn't have anu effect on search. That's why you need condition to see will you include this column in where clause in the first place.
I'm a beginner with pentaho data integration and I want to split a string with the following form : FIRSTNAME LASTNAME CODE
I want to isolate the firstname and lastname from the code noting that the lastname can contain more than a word.
I thought about spliting all the string based on space separator but the problem is that the name can sometimes be composed of more than two words.
Can you show me please the steps to follow to acheive that?
Split the rows with Step "Split Fields". Then concatenate the fields for lastname1 or lastname2OrCode if person has 2 last names, otherwise set the code field.
And this simple Javascript (Do not forget to click at Get variables)
var lastname;
var code;
if(codeTmp==null){
code = lastname2OrCode;
lastname= lastname1;
}else {
lastname = lastname1+ " "+ lastname2OrCode;
code = codeTmp;
}
I need to create a datasource for a combobox using a Linq to SQL query.
i.e. cboFindPerson.DataSource = LQPersonList
(where LQPersonList is a query)
But this time, I need to first concatenate the LastName and FirstName fields, and then order by the FullName, like this.
'-- Create the first Query with concatenation
Dim LQ = From b In DCAppMain.tblPeopleMain
Where b.PeopleID = lngPeopleID And b.CurrentEmployee = True
Select FullName = b.LastName & ", " & b.FirstName, b.PeopleID
'-- Create the 2nd Query based on first so I can order by FullName
Dim LQPersonList = From c In LQ
Order By c.
But when I get to c., intellisense says no fields are available.
I've written queries based on queries before w/o issue. I've also concatenated fields w/o issue. But apparently putting the two together is an issue.
I've been searching on this for several hours now, but can't find an answer that is on target.
Instead of using:
Dim LQ = From b In DCAppMain.tblPeopleMain
Where b.PeopleID = lngPeopleID And b.CurrentEmployee = True
Select FullName = b.LastName & ", " & b.FirstName, b.PeopleID
you need to use:
Dim LQ = From b In DCAppMain.tblPeopleMain Where b.PeopleID = lngPeopleID And
b.CurrentEmployee = True Select New With{.FullName = b.LastName & ", " &
b.FirstName, .PeopleID = b.PeopleID}
this is because when you pull individual properties out of a LINQ statement you end up with an IEnumerable(Of AnonymousType) which is fine but if you want intellisense to pick up the values that you are pulling out of your collection of objects then you need to assign them names.
So using the second LINQ statement you are basically creating a constructor for the anonymous type that also defines the properties FullName and PeopleID on the anonymous objects that make up the collection returned by the LINQ statement.
I am trying to write some code that uses SQL to delete rows from several tables.
A user would type type numbers into a textbox that are separated by a comma which is used in the WHERE clause of a SQL DELETE statement.
I have managed to split the string into a variant array and now I want to insert it into my SQL statement.
How do I insert the variable into the SQL statement and have it run through every element of the array?
EDIT: A bit more digging has taught me about For Each Next statements. This is probably what im looking for.
I suggest you build your query in VBA, then your list of numbers can be an IN statement:
sSQL = "DELETE FROM table WHERE ID In (" & MyList & ")"
Where MyList = "1,2,3,4" or such like.
As you can see, you do not need an array and a textbox would be more suitable than a combobox. If you wish to allow your users to select by say, a name, then a listbox is useful. You can iterate through the selected items in the listbox and build a string from IDs to be used in the Delete statement. ( MS Access 2007 - Cycling through values in a list box to grab id's for a SQL statement )
You can then execute the sql against an instance of a database. For example:
Dim db As Database
Set db = CurrentDB
db.Execute sSQL, dbFailOnError
MsgBox "You deleted " & db.RecordsAffected & " records."
A generic approach
WHERE
','+Array+',' like '%,'+col+',%'
It will consider all the numbers available in your Array
You could make it simple and elaborate a string, something like
stringBuilder sb = StringBuilder("DELETE FROM YOURTABLE WHERE ");
foreach(string st in stringArray){
sb.append("YOURFIELD='" + st + "'");
//If it is not the last element, add an "OR"
if (st != stringArray[stringArray.length -1]) sb.append(" OR ");
}
//Now, you have a string like
//DELETE FROM YOURTABLE WHERE YOURFIELD='hello' OR YOURFIELD='YES'
//... do something with the command
This method will fail if you want to run SQL query on two (or multiple) columns using array values from two different arrays. .e.g
where col1=array1(i) and col2=array2(i)
I'm using VBA excel 2003,SQL 2005 to make a sql query call and inside my sql statement I'm using '+' operator to concatenate two strings.
dim query as string
query = "Select distinct ', '+emailaddress1 "
query = query & "from contact "
would this work inside vba? My query returns too many records in excel but not in SQL?
Please just focus on this 2 lines of code and not worry about the rest of my sql call, I'm just wondering whether or not this specific string would work?
Your code will return a column where each row would be an email address with a comma in front of it. If that is what you want, then yes, it will work.
If, on contrary, you want a single string where all email addresses would be listed, separated with commas, that'd be
query = "declare #foo varchar(max);"
query = query & "select distinct #foo = isnull(#foo,'') + emailaddress1 + ', ' from contact;"
query = query & "select left(#foo, len(#foo)-2);"