LINQ And Anonymous Type in VB.Net

Recently I am having a problem in converting an sql query to linq against EF in vb.net win forms 4.0

See my previous post for the particular query, so i posted the question to SO in which i got response in form of a link further with the help of that particular link i was able to find another link in asp.net forum using that i am able to find arrive the expected output in linq .

So Everything is fine at last, but the answer contained in the referred link for my question in stack overflow contains a keyword “KEY” while grouping the EF table. And that throws me compile time error in vb.net, so i have to search for that and find the answer in the asp.net forum which omits the “KEY” keyword. Essentially i just want to find out what happened to the referred answer and answer i got. So i just did a search in MSDN which strangely shows lot links from SO site itself. Actually i can say that the results are much more better than the Google result (strange). And out the links this one was exactly reflects my question. The accepted answer is also the answer which i have found from other links. But oddly enough  this , this and this links are raising the same question which i have got in mind when working through the solutions. And the only man who answered all the three question is Jon Skeet , It seems that the key keyword is mutable in vb.net and by default it is immutable in C#

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created.[1] This is in contrast to a mutable object, which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change but the object’s state appears to be unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.

Immutable objects are often useful because they are inherently thread-safe.[1] Other benefits are that they are simpler to understand and reason about and offer higher security than mutable objects.[1] 

The above mentioned paragraph is taken from wikipedia which obviously states the difference between mutable and immutable objects.

so to my understanding when you are creating an anonymous type in c# the the variable which is holding the output is immutable i.e. you don’t have to declare the key keyword  , but in vb.net you have to make the object immutable explicitly .

from what i understand from this msdn link if you are declaring an anonymous type you should consider using KEY , which can be correlated to the primary key of sql database so that you can  compare or get Hashcode values , if there is no key is declared while using anonymous type you cannot compare , get max value or group or Distinct effectively ,simply they wont give you any proper results.

so how to get the key to be displayed while grouping  your vb.net anonymous type

that’s i am yet to find. (to be continued…)

Adding Default Value or Empty Value to Entity Framework bound Combo box

Another day , and another learning, I was trying to add a default value to a combo box bound to an EF result list. Normally we would just create a generic list or some type of your favourite collection from the database and add a empty entry or default entry like “Select All” to the list item with index as 0.

The same applies here the only difference is you have to create a temporary entity object (table) which is going to bind the combo box and add default value to its key and other required parameter. Then bind it to the combo box. The code is given here

Its in C#, the VB version is here

  Using context As New SPEntities1
            Dim supplier = context.tblSuppliers.Where(Function(x) x.rec_status = "A" And x.type = "S").OrderBy(Function(o) o.supName)
            Dim lst = supplier.ToList
            Dim defaultEntry = New tblSupplier
            defaultEntry.supName = "SELECT"
            defaultEntry.supCode = "0"
            lst.Insert(0, defaultEntry)

            cmbSupplier.DataSource = lst
            cmbSupplier.DisplayMember = "supName"
            cmbSupplier.ValueMember = "supCode"

        End Using

 

Thanks to Stackoverflow and the OP and ALL Respondents.

Change Datatype of Datacolumn

converting datatype of datacolumn in a datatable at runtime can be done in two methods
1) while filling in adapter
da.SelectCommand = cmd
da.FillSchema(dt, SchemaType.Source)
dt.Columns(6).DataType = GetType(String)
dt.Columns(7).DataType = GetType(String)
dt.Columns(8).DataType = GetType(String)

da.Fill(dt)
2) if your datalayer returns only datatable then here is the second method
Dim tempdt As DataTable = dt.Clone
tempdt.Columns(6).DataType = GetType(String)
tempdt.Columns(7).DataType = GetType(String)
tempdt.Columns(8).DataType = GetType(String)
For Each r As DataRow In dt.Rows
tempdt.ImportRow(r)
Next

Search Inside Datagridview Winforms VB.NET

If txtName.Text <> "" Then
            Dim s As String = txtName.Text.Trim
            dgv.ClearSelection()
            dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            Dim someText As String = s
            Dim gridRow As Integer = 0
            Dim gridColumn As Integer = 0
            For Each Row As DataGridViewRow In dgv.Rows
                For Each column As DataGridViewColumn In dgv.Columns
                    Dim cell As DataGridViewCell = (dgv.Rows(gridRow).Cells(gridColumn))
                    If cell.Value.ToString.ToLower.Contains(someText.ToLower) Then
                        ‘cell.Style.BackColor = Color.Yellow
                        dgv.FirstDisplayedScrollingRowIndex = Row.Index
                        dgv.Item(cell.ColumnIndex, cell.RowIndex).Selected = True

                    End If
                    gridColumn += 1
                Next column
                gridColumn = 0
                gridRow += 1
            Next Row
        Else
            dgv.ClearSelection()
        End If

here dgv is datagridview and txtname is the textbox

when you started to type letters in txtname the rows inside the datagridview will be selected based on the letters typed .