Enumerate 'columns' in Entity Framework - vb.net

we are dipping our toes with EF4 and have hit a wall.
We have an application that manages member data and a core requirement is to generate letters using Word mail merge.
In the current implementation we generate a dataset for the required records and enumerate it's columns and rows to create a tab delimited text file that Word can use to merge from. (This works really well, the Tab is used as there are multi-line results in the file such as FormattedAddressBlock).
With EF4 there is no way to convert internally to a dataset (this feature was dropped after the Orcas beta), so to the question:
How do we enumerate the columns and rows of a LINQ or ObjectQuery to allow us to build the text file.
Here is a sample of code we use with the datatable.
Using wmTextWriter As TextWriter = New StreamWriter(WordMergeDataFile)
' Write the header row to the merge data file.
Dim wmOutput As New StringBuilder()
For i As Integer = 0 To MergeData.Columns.Count - 1
wmOutput.Append(MergeData.Columns(i).ColumnName & vbTab)
Next
wmOutput.Remove(wmOutput.Length - 1, 1) ' Remove the trailing Tab character.
wmTextWriter.WriteLine(wmOutput.ToString)
' Loop through the datatable and write out the data rows to the work merge file.
wmOutput = New StringBuilder()
With MergeData
For iRowNo As Integer = 0 To .Rows.Count - 1
For iColNo As Integer = 0 To .Columns.Count - 1
wmOutput.Append(DOUBLEQUOTE & .Rows(iRowNo).Item(iColNo).ToString & DOUBLEQUOTE & vbTab)
Next
wmOutput.Remove(wmOutput.Length - 1, 1) ' Remove the trailing Tab character.
wmTextWriter.WriteLine(wmOutput.ToString)
wmOutput = New StringBuilder()
Next
End With
wmOutput = Nothing
wmTextWriter.Close()
End Using
Any help would be much appreciated.
Mark Harby
Nottingham. UK

Related

export SQL data to a csv file from ASP.net

I have a view in the database, i would like to call that view and export the data to a Csv file. I have used the following link to come up with a solution however my view returns a few columns which has values with commas so while opening the csv i get the error the csv is unsafe, might by a sylk file etc. Once i open it there is no date in the CSV.
The link i used
https://www.aspsnippets.com/Articles/Export-data-from-SQL-Server-to-CSV-file-in-ASPNet-using-C-and-VBNet.aspx
I am using VS 2017
SQL and
VB.Net
Thanks in advance
That code is a bit dodgy actually, because it is explicitly replacing commas in data with semicolons and it is also adding an extra comma to the end of each line. Here's concise way to generate CSV data from a DataTable with quoted field values:
Dim csv = String.Join(Environment.NewLine,
myDataTable.AsEnumerable().
Select(Function(row) """" &
String.Join(""",""",
row.ItemArray) &
""""))
If you're not au fait with LINQ, here's a more conventional way to do the same thing:
Dim csv As String
For rowIndex = 0 To myDataTable.Rows.Count - 1
'Add a line break before all but the first line.
If rowIndex > 0 Then
csv &= Environment.NewLine
End If
Dim row = myDataTable.Rows(rowIndex)
'Add a double-quote before the first field value.
csv &= """"
For columnIndex = 0 To myDataTable.Columns.Count - 1
'Add a closing double-quote, a delimiting comma and an opening
' Double-quote before all but the first field value.
If columnIndex > 0 Then
csv &= ""","""
End If
csv &= row(columnIndex).ToString()
Next
'Add a double-quote after the last field value.
csv &= """"
Next

Excel VBA - extract parts of string to form a list?

I'm trying to extract some details from some SQL code in order to make a list - specifically: I'm trying to extract nominal codes from a case statement to make a human readable list of nominal codes...I'm wondering if there's a way for VBA to extract the string parts and also output a list?
Here's the code that, for example, we'll say is in cell a1...
when ProfitAndLoss.acno in ('P01200','P01201','P01205','P01206','P01210','P01211','P01220','P01221','P01225','P01226','P01230','P01231','P01235')then 'DirSals'
What I need is...
P01200
P01201
P01205
etc
You want to use the Split function.
Option Explicit
Sub makeList()
Dim parts As Variant
Dim nextLine As Long
Dim i As Long
nextLine = 2
parts = Split(Cells(1, 1).Value, "'")
For i = LBound(parts) + 1 To UBound(parts) - 2 Step 2
Cells(nextLine, 1).Value = parts(i)
nextLine = nextLine + 1
Next i
End Sub
This splits the string up into sections with ' as the delimiter. Then it loops through each part, skipping the first part - when ProfitAndLoss.acno in ('- and the last two parts - ')then' and 'DirSals'. I used step two because each second slice is '-'.
Each part is output onto a new line, incremented each time.

Save Word document as a filename generated from tabled info contained within the document

I am looking to enable Word to save with a file name using data contained within the document.
At the top of the document (an airline release letter), there is a table containing 2 columns with 3 rows containing alpha in one column and alpha-numeric data in column 2.
Column 1,
Cell 1: AETC; Cell 2: MAWB; Cell 3: HAWB
Column 2,
Cell 1: 80123; Cell 2, 0161234567; Cell 3: 00112345678
Basically, the first column will be the static labels for the variable data to be entered into column 2.
From all this, I want to generate a save-as file name: AETC80123_MAWB0161234567_HAWB00112345678_ReleaseLetter.doc
I've barely scratched the surface of VBA as I am more an operations supervisor than a techie so I'm not certain if this is even possible.
Any help/direction/copy-paste coding (if it's super easy and of little trouble) would be awesome!
Thanks!
I am not going to work it all out in detail for, nor is this tested at all (just written out of my head), but this should give a hint how you read cell content in a Word document:
' Set tbl to first table in document
Dim tbl As Table
Set tbl = ActiveDocument.Tables(0)
Dim r As Integer
Dim c As Integer
Dim val As String
Dim filename As String
filename = ""
For r = 1 To tbl.Rows.Count
For c = 1 To tbl.Columns.Count
' Get text in cell
val = tbl.Cell(r, c).Range.Text
' and append to string or whatever
filename = filename & val & "_"
Next c
Next r
Finally, save your document using
ActiveDocument.SaveAs FileName:=filename
Check this microsoft site for more information about SaveAs parameters.

Creating an Array from a CSV text file and selecting certain parts of the array. in VB.NET

Ive searched over and over the internet for my issue but I havent been able to find / word my searches correctly...
My issue here is that I have a Comma Separated value file in .txt format... simply put, its a bunch of data delimited by commas and text qualifier is separated with ""
For example:
"So and so","1234","Blah Blah", "Foo","Bar","","","",""
"foofoo","barbar","etc.."
Where ever there is a carriage return it signifies a new row and every comma separates a new column from another.
My next step is to go into VB.net and create an array using these values and having the commas serve as the delimeter and somehow making the array into a table where the text files' format matches the array (i hope im explaining myself correctly :/ )
After that array has been created, I need to select only certain parts of that array and store the value into a variable for later use....
Andthats where my trouble comes in... I cant seem to get the correct logic as to how to make the array and selecting the certain info out of it..
If any
You might perhaps give a more detailed problem description, but I gather you're looking for something like this:
Sub Main()
Dim fileOne As String = "a1,b1,c1,d1,e1,f1,g1" + Environment.NewLine + _
"a2,b2,c2,d2,e2,f2,g2"
Dim table As New List(Of List(Of String))
' Process the file
For Each line As String In fileOne.Split(Environment.NewLine)
Dim row As New List(Of String)
For Each value In line.Split(",")
row.Add(value)
Next
table.Add(row)
Next
' Search the "table" using LINQ (for example)
Dim v = From c In table _
Where c(2) = "c1"
Console.WriteLine("Rows containing 'c1' in the 3rd column:")
For Each x As List(Of String) In v
Console.WriteLine(x(0)) ' printing the 1st column only
Next
' *** EDIT: added this after clarification
' Fetch value in row 2, column 3 (remember that lists are zero-indexed)
Console.WriteLine("Value of (2, 3): " + table(1)(2))
End Sub

adapting combination code for larger list

I have the following code to generate combinations of string for a small list and would like to adapt this for a large list of over 300 string words.Can anyone suggest how to alter this code or to use a different method.
Public Class combinations
Public Shared Sub main()
Dim myAnimals As String = "cat dog horse ape hen mouse"
Dim myAnimalCombinations As String() = BuildCombinations(myAnimals)
For Each combination As String In myAnimalCombinations
''//Look on the Output Tab for the results!
Console.WriteLine("(" & combination & ")")
Next combination
Console.ReadLine()
End Sub
Public Shared Function BuildCombinations(ByVal inputString As String) As String()
''//Separate the sentence into useable words.
Dim wordsArray As String() = inputString.Split(" ".ToCharArray)
''//A plase to store the results as we build them
Dim returnArray() As String = New String() {""}
''//The 'combination level' that we're up to
Dim wordDistance As Integer = 1
''//Go through all the combination levels...
For wordDistance = 1 To wordsArray.GetUpperBound(0)
''//Go through all the words at this combination level...
For wordIndex As Integer = 0 To wordsArray.GetUpperBound(0) - wordDistance
''//Get the first word of this combination level
Dim combination As New System.Text.StringBuilder(wordsArray(wordIndex))
''//And all all the remaining words a this combination level
For combinationIndex As Integer = 1 To wordDistance
combination.Append(" " & wordsArray(wordIndex + combinationIndex))
Next combinationIndex
''//Add this combination to the results
returnArray(returnArray.GetUpperBound(0)) = combination.ToString
''//Add a new row to the results, ready for the next combination
ReDim Preserve returnArray(returnArray.GetUpperBound(0) + 1)
Next wordIndex
Next wordDistance
''//Get rid of the last, blank row.
ReDim Preserve returnArray(returnArray.GetUpperBound(0) - 1)
''//Return combinations to the calling method.
Return returnArray
End Function
End Class
'
CHANGES//
For wordDistance = 1 To inputList.Count.ToString / 2
Dim count = inputList.Count.ToString
'Go through all the words at this combination level...
For wordIndex As Integer = 0 To inputList.Count.ToString - wordDistance
'Get the first word of this combination level
combination.Add(inputList.Item(wordIndex))
'And all all the remaining words a this combination level
For combinationIndex As Integer = 1 To wordDistance
combination.Add(" " & inputList.Item(wordIndex + combinationIndex))
Next combinationIndex
'Add this combination to the results
If Not wordsList.Contains(combination) Then
wordsList.Add(combination.ToString)
End If
'Add a new row to the results, ready for the next combination
'ReDim Preserve returnArray(returnArray.GetUpperBound(0) + 1)
Next wordIndex
Next wordDistance
One obvious thing in your code is the usage of ReDim Preserve. That can be quite a slow operation since I think it copies the whole array into a new array every time the size is changed, and since you're doing that inside loops I assume that could be a significant issue.
The simplest way of fixing that is to stop using those kinds of arrays and instead use List with it's Add method.
I want to make sure I understand what you are trying to do first. Your problem seems to be:
Given a list of strings,
Return every possible combination of n items from the list,
where n = 2 to length of list
For example, in a list of 5 strings, you would want all combinations of 2 strings, of 3 strings, of 4 strings, and of 5 strings.
If that is an accurate statement of your problem, there is one glaring issue to point out. The number of items you will be generating is on the order of 2 ^ (length of list). This means that trying to generate all combinations of 300 items will never be fast no matter what. Also, for any but the tiniest of lists, you will need to generate items lazily or you will run out of memory.
If you do not want all combinations of all lengths, you may want to clarify your question to better state your desired goal.