I have an Object and I am trying to populate it with a passed list of records, the issue I am having is I want to do a For each loop for each record and then return the all records and the end of the function.
What it is currently doing:
StoreRec is currently passing 3 records
It is writing only first record to the DB because there is no loop
What I want to accomplish:
I want to be able to write multiple lines on the DB
Input: StoreRec
Store1,
Bruise Store,
Germany,
Store2,
Jakes Store,
Bromol,
Store3,
Fred's Store,
Cornices
Current Output: StoreRecord
Store1,
Bruise Store,
Germany
Expected output: StoreRecord
Store1,
Bruise Store,
Germany ,
Store2,
Jakes Store,
Bromol,
Store3,
Fred's Store,
Cornices
Current populate Function:
Public Function PopulateStoreRecords(StoreRec As Dictionary(Of String, String)) As Object
Dim StoreRecord As New StoreDetailRecord
If StoreRec.ContainsKey("Store Number") Then
StoreRecord.StoreNumber = StoreRec.Item("Store Number")
End If
If StoreRec.ContainsKey("Store Name") Then
StoreRecord.StoreName = StoreRec.Item("Store Name")
End If
If StoreRec.ContainsKey("Store Location") Then
StoreRecord.StoreLocation = StoreRec.Item("Store Location")
End If
Return StoreRecord
End Function
Related
How can i check which barcheckitem is checked in a certain group?
I am dinamicaly creating check items in a BarSubItem.
There are 2 values i am using to create a certain checkitem: item.naziv_programa and item.tabela.
This is part of code which creates items:
For Each item As ReturnList In mysql.vratiPrograme()
Dim barmanager1 As New BarManager
Dim BarCheckItem = New BarCheckItem()
BarCheckItem.Content = item.naziv_programa
BarCheckItem.Name = item.tabela
BarCheckItem.GroupIndex = 1
Program.Items.Add(BarCheckItem)
Next
I need to run another function on buttonclick in which i will need to pass "Name" of the checked checkitem as a string:
Public Function vratiOpcijePrograma(ByVal tabela As String)....
I need to find the unique values from a list and the number of times each value occurred in the original list. Here is what i have so far:
Dim Lister As New List(Of String)()
For Each item In eColumn
Lister.Add(item.value)
Next
Dim Result As New List(Of String)
Result = Lister.Distinct().ToList
For Each st In Result
MsgBox(st)
Next
The result is a list of all the unique values, but does not include a count for each item. For example, if my list was
John
John
Abbey
Larry
Larry
Larry
Charles
I want 4 values returned: John = 2, Abbey = 1,Larry = 3, Charles = 1.
Using linq's .Distinct() is only going to give you a list containing each distinct name in your list; so as you must have seen when your messagebox loop ran, it just shows you each name that is in your list once.
VB's lists do not have a native function for returning the count of an item's occurrences in a list, so to achieve your desired result just group them using linq's .GroupBy() function. It will return a Linq.GroupedEnumerable object, which can be iterated through and also possesses the kind of count property you're looking for:
Dim myList As New List(Of String) From {"John", "John", "Abbey", "Larry", "Larry", "Larry", "Charles"}
Dim groupedNames = myList.GroupBy(Function(x) x)
If groupedNames IsNot Nothing AndAlso groupedNames.Count > 0 Then
For Each person In groupedNames
Debug.Print(person.Key & "-" & person.Count.ToString)
Next
End If
Output:
John-2
Abbey-1
Larry-3
Charles-1
I'll post below the file I'm given for school. Basically I have a stock amount and a sale price. I have to figure out how to show the value for each
item. The only I would know how to do this is by doing one at a time.
Stock#, Descrip, Sale Price, Iventory
ET001, 1 Drawer End table, 169.99, 10
ET002, 2 Drawer End table, 289.95, 05
ET003, 6 Drawer End table, 129.99, 02
ST001, Stacking Table, 320.75, 20
You will still need to read in the file and parse the contents. I would suggest using a StreamReader inside a Using block to read each line of text and then .Split(",") the line data to get the values. Once you have the values parsed it should be fairly straight forward using this simple console application as a starting point.
Option Strict On
Option Explicit On
Module Module1
Sub Main()
' Create a list to hold the items
Dim slist As New List(Of StockItem)
' Get your file contents and parse out the data then add it to the list. Done manually below
slist.Add(New StockItem With {.StockCode = "ET001", .Description = "1 Drawer End table", .SalePrice = 169.99D, .Inventory = 10I})
slist.Add(New StockItem With {.StockCode = "ET002", .Description = "2 Drawer End table", .SalePrice = 289.99D, .Inventory = 5I})
'Loop Through all Items and get the values.
For Each item As StockItem In slist
'Write it out to the Debug
Debug.WriteLine(item.GetValue())
Next
End Sub
End Module
Public Structure StockItem
Public Property StockCode As String
Public Property Description As String
Public Property SalePrice As Decimal
Public Property Inventory As Integer
Public Function GetValue() As Decimal
Return CDec(Me.SalePrice * Me.Inventory)
End Function
End Structure
Iam frustrated to accomplish a simply population of a combobox, below I have added one new item to the combobox everything seems to be fine.
Question 1 : But how could I get there the information's from the sql query, without having to add it all manually. [ I suppose by simply adding Items.Add line to the while loop ], but here is another thing - The start data is a database record previewer, So it is Simple Name Simple Surname [/] with a dropdown menu with all customers,
Question 2. The data I get from the mysql result is id,name,surname how to point it as the current displayed name/surname and for later purposes - like a update get the selected id of another customer from the dropdown? I don't need the insert command or code just need the information how can I get the id from a selection. If something is unclear don't hesitate to ask.
'Select the first item ( the selection would be a ID of the customer which isn't the index at all)
ComboBoxEdit.SelectedIndex = 0
Form
Dim properties As DevExpress.XtraEditors.Repository.RepositoryItemComboBox = _
ComboBoxEdit.Properties
properties.Items.Add(New Customers(1, "Ta", "t").ToString)
'Select the first item ( the selection would be a ID of the customer which isn't the index at all)
ComboBoxEdit.SelectedIndex = 0
Getting customers into Class Customers ( I guess its that way I need to do it )
Public Function init_customers()
' Create a list of strings.
Dim sql As String
Dim myReader As MySqlDataReader
con.Open()
sql = "select * from customers"
'bind the connection and query
With cmd
.Connection = con
.CommandText = sql
End With
myReader = cmd.ExecuteReader()
While myReader.Read()
list.Add(New Customers(myReader.GetInt64(0), myReader.GetString(1), myReader.GetString(2)))
End While
con.Close()
'Return list
End Function
The class customers
Public Class Customers
Public Sub New(ByVal id As Integer, ByVal name As String, ByVal surname As String)
Me.ID = id
Me.Imie = name
Me.Nazwisko = surname
End Sub
#Region "Get/Set"
Public Property ID() As Integer
Get
Return Me._id
End Get
Set(ByVal value As Integer)
Me._id = value
End Set
End Property
Public Property Imie() As String
Get
Return Me._imie
End Get
Set(ByVal value As String)
Me._imie = value
End Set
End Property
Public Property Nazwisko() As String
Get
Return Me._nazwisko
End Get
Set(ByVal value As String)
Me._nazwisko = value
End Set
End Property
Public ReadOnly Property Surname() As Decimal
Get
Return Me._nazwisko
End Get
End Property
Public Overrides Function ToString() As String
Return _imie + " " + _nazwisko
End Function
#End Region
Private _id As Integer
Private _imie As String
Private _nazwisko As String
End Class
=========== Edit 2 =====================
Ok my dropdown is populated
As I said this is a record preview form so how can I get now the default selection of the combobox.
The thing is I pass there a string
Form1.GridView1.GetRowCellValue(Form1.GridView1.FocusedRowHandle, "Wystawione_na").ToString()
This code returns me SimpleName SimpleSurname - as a one string
Same method is applied to combobox display.
How can I get now the Id of the item, it has to somehow compared and returning a id so it could be set cmbx.SelectedIndex = 0 as the id of customer selection
I take a simpler route, not sure if it's the most efficient though:
Dim Conn As New SqlConnection
Conn.ConnectionString = sYourConnectionString
Conn.Open()
Dim da As New SqlDataAdapter("select * from customers", Conn)
Dim ds As New DataSet
da.Fill(ds, sSql)
cmbxCustomers.DataSource = ds.Tables(0)
cmbxCustomers.ValueMember = "ID" 'or whatever column you want
Of course, I normally use a wrapper class to do almost all of the above code, but the last two lines apply to part of your question.
As far as retrieving that data later based on the ID selected: well you can simply use a DataSet (or DataTable, my preference) class member variable so the data is stored the from the initial load and iterate through the table looking for the row that matches the ID you're wanting information from.
I am using this to get all my records ordered by date:
Dim myData = db.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID).OrderBy(Function(x) x.Exercise_Create_Date)
I loop through with this:
For Each i As Tbl_Exercise In myData
data.Add(New Object() {i.Tbl_Exercise_Type.ExType_Desc, i.Exercise_Duration})
Next
How can I group by i.Tbl_Exercise_Type.ExType_Desc so I don't have duplicates?
I tried this:
Dim myData = db.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID).GroupBy(Function(x) x.Exercise_Type_ID)
But, I don't know how to access the variables within my loop.
Thank you.
Edit:
<EmployeeAuthorize()>
Function ExercisePieChartData() As JsonResult
' get current employee's id
Dim db1 As EmployeeDbContext = New EmployeeDbContext
Dim user1 = db1.Tbl_Employees.Where(Function(e) e.Employee_EmailAddress = User.Identity.Name).Single()
Dim empId = user1.Employee_ID
Dim activityGroups = db.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID = empId).GroupBy(Function(x) x.Exercise_Type_ID)
Dim data = New List(Of Object)
' columns (headers)
data.Add(New Object() {"Type", "Duration"})
' Iterate through each collection of activities, grouped by activity types
For Each group In activityGroups
' Iterate through each Tbl_Exercise in the group
For Each activity In group
' Do something with an individual element
data.Add(New Object() {activity.Tbl_Exercise_Type.ExType_Desc, activity.Exercise_Duration})
Next
Next
Return Json(data, JsonRequestBehavior.AllowGet)
End Function
Edit:
' Iterate through each collection of activities, grouped by activity types
For Each group In activityGroups
Dim type = ""
Dim duration = 0
' Iterate through each Tbl_Exercise in the group
For Each activity In group
' Do something with an individual element
type = activity.Tbl_Exercise_Type.ExType_Desc
duration += activity.Exercise_Duration
Next
data.Add(New Object() {type, duration})
Next
Note: Your example isn't entirely clear, so I'm going to make a few assumptions about what you are trying to do with my solution. I'll modify it as needed as more details are made available.
From what it looks like, for each employee you are trying to get a collection of types of exercise that they have performed, and with each type of exercise you would also like a collection of the lengths of time that they spent performing these activities.
I'm going to assume that a row in Tbl_Exercises looks like this:
Public Class Tbl_Exercise
Public Property Exercise_Employee_ID As Integer
Public Property Exercise_Type_ID As Integer
Public Property Exercise_Type_Desc As String
Public Property Exercise_Duration As Integer
End Class
The attempt you make at the end is pretty close, you just need to actually use the grouping once you've created it.
What the GroupBy method has returned you here is actually an IGrouping(Of Integer, Tbl_Exercise) where the key is the Exercise_Type_ID and the value is a collection of rows that are grouped by the key. To access them its pretty simple, you just have to think of the groupings as a sort of nested collection:
Dim activityGroups = db.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID = employeeId).GroupBy(Function(x) x.Exercise_Type_ID)
' Iterate through each collection of activities, grouped by activity types
For Each group In activityGroups
' Iterate through each Tbl_Exercise in the group
For Each activity In group
' Do something with an individual element
Console.WriteLine(activity.Exercise_Duration)
Next
Next
Obviously, you may want to do something different than print out individual collections, you may want to data bind each grouping to their own DataGrid so that you can display them separately. You may want to sort each grouping by date, or you may only want the first element from each group. Once you understand how the groupings are structured, it should be pretty easy to accomplish exactly what you're looking for.
Edit:
If you're just looking to aggregate some field in the grouping, this is very simple to accomplish without needing to resort to looping over the results. Simply adding a call to Select on top of your GroupBy will let you project the results into your desired aggregate form:
Dim data = db.Tbl_Exercises _
.Where(Function(x) x.Exercise_Employee_ID = empId) _
.GroupBy(Function(x) x.Exercise_Type_Desc) _
.Select(Function(x)
Return New With {
' x.Key will give you access to the value that the grouping is grouped by
.Exercise_Type_Desc = x.Key,
.TotalDuration = x.Sum(Function(y) y.Exercise_Duration)
}
End Function)