How to filter list in asp.net/vb.net using linq - asp.net

I have a list of class
How can I filter on some condition..I am applying following it is working when value gets exact match
Dim result = obj.OfType(Of clsEmrItmMstr)().Where(Function(s) s.GenName Like txtserach.Text)
grddetails.DataSource = result
grddetails.DataBind()
where "clsEmrItmMstr" is my class name and "GenName" is field in class

Instead of the Like operator you could simply use String.Contains:
Dim result = obj.OfType(Of clsEmrItmMstr)().
Where(Function(s) s.GenName.Contains(txtserach.Text))
With Like you need * as wildcard, so this should work:
Dim result = obj.OfType(Of clsEmrItmMstr)().
Where(Function(s) s.GenName Like String.Format("*{0}*", txtserach.Text))
(assuming that you want to find all objects where the GenName contains the text entered in the TextBox)

You can use Contains function
Dim result As dynamic = obj.OfType(Of clsEmrItmMstr)().Where(Function(s) s.GenName.Contains(txtserach.Text))
grddetails.DataSource = result
grddetails.DataBind()

Related

How to select specific index in a multidimensional array in asp.net using vb

I cannot explain well in the title so i'll explain here the scenario:
In here, i have a query that will be placed in an array.
Dim arrUsers As Object = {{sqlReader("dephead"), dropdown1}, {sqlReader("mm"), dropdown2}
I want to enable the dropdown object if it matches my if else condition:
'indexes1= the 1st indexes of each pair in the array (the sqlReaders)
'indexes2= my dropdown objects in my user control
For Each indexes1 In arrUsers
If Session.Item("EmployeeID") = indexes1 Then
indexes2.Enabled = True
End If
Next
so lets say the my session id will be "dephead", then the dropdown1 will be enabled. Thats what i would like to happen but im not familiar on arrays so kindly help me. Thanks.
This should work:
For i As Integer = 0 to arrUsers.GetUpperBound(0)
If Session.Item("EmployeeID") = arrUsers(i)(0) Then
arrUsers(i)(1).Enabled = True
End If
Next
You simply give the array two indexes instead of one. arrUsers(0)(1), for example. The first index returns the object at that index, and the second index returns the object inside the previously returned array. arrUsers(0)(1) will first grab the array at 0 index ({sqlReader("dephead"), dropdown1}), then get the object at index 1 (dropdown1).
This answered my question. The answer above helped me and i just changed the declaration of my arrays and changed some syntax.
Dim arrUsers(,) As Object =
New Object(,) {
{sqlReader("dephead"), dropdown1}, _
{sqlReader("mm"), dropdown2}, _
}
Dim bound0 As Integer = arrUsers.GetUpperBound(0)
Dim bound1 As Integer = arrUsers.GetUpperBound(1)
For i As Integer = 0 To bound0
If Session.Item("EmployeeID") = arrUsers(i, 0) Then
arrUsers(i, 1).Enabled = True
End If
Next

Looping through different dropdown lists

I have multiple controls on a page that are all similar and are all numbered. For instance, I have multiple month controls like this:
Replacement1MonthDropDownList
Replacement2MonthDropDownList
Replacement3MonthDropDownList
Etc.
But when I have common code that works on all of the controls, I need a big Select Case statement like this:
Select Case Count
Case 1
Call Me.FillReplacements(rf.Replacements(0), Me.Replacement1MonthDropDownList, Me.Replacement1AmountTextBox, Me.ReplacementSaveButton)
Case 2
Call Me.FillReplacements(rf.Replacements(0), Me.Replacement1MonthDropDownList, Me.Replacement1AmountTextBox, Me.ReplacementSaveButton)
Call Me.FillReplacements(rf.Replacements(1), Me.Replacement2MonthDropDownList, Me.Replacement2AmountTextBox, Me.SplitButton1)
Is it possible to loop through the controls and get them by name--justreplacing the numbers in the name with the current Count in my loop?
Sorry, I'm very new to Visual Basic! :S
Yes, you can. The Page class (Me, in this case) has a FindControl method which allows you to find a control by name. So, for instance, you could do something like this:
Dim monthControl As Control = Me.FindControl("Replacement" & Count.ToString() & "MonthDropDownList")
Dim splitControl As Control = Me.FindControl("SplitButton" & Count.ToString())
If you need to cast them as a more specific type, you could use DirectCast. For instance:
Dim monthControl As DropDownList = DirectCast(Me.FindControl("Replacement" & Count.ToString() & "MonthDropDownList"), DropDownList)
Alternatively, and perhaps preferably, you could make an array of controls so you could access them by index. For instance, if you had an array like this defined:
Private monthControls() As DropDownList = {Replacement1MonthDropDownList, Replacement2MonthDropDownList, Replacement3MonthDropDownList}
Then you could access it by index like this:
Dim currentMonthControl As DropDownList = monthControls(Count)

calling list item where var = value

is there a way of calling a specific list of lists without having to check (or loop) them all?
it is easier to understand with an example....
lets say
callList(5).key = "1234"
callList(5).callOpened = "11/26/13"
now i want to do something like
textbox_callOpened.text = callList(where key = "1234").callOpened
i also need to know what index that was at for there are many more items that i need to output too.
You can use LINQ. Add Imports System.Linq at top of the file and use First method with lambda expression as a predicate to get what you need:
' that gives you item matching your predicate '
Dim item = callList.First(Function(x) x.Key = "1234")
' you can use it to set the property '
textbox_callOpened.text = item.callOpened
pseudo code, didn't test but should work
dim something = callList.firstordefault(function(d) d.key = "1234")
if something is not nothing then
textbox_callOpened.text = something.callOpened
else
'cant find an element with key 1234
end if

How to set the values for HatchBrush from config file in asp.net?

In my class, i have used the following code
'Draw text
hb = New HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray)
Now i would like to get the values assigned to HatchBrush from an xml file as follows, instead of directly assigning it in the class as above. Because there is a need to change the colors frequently.
<hatchstyle>HatchStyle.LargeGrid</hatchstyle>
<forecolor>Color.LightGray</forecolor>
<backcolor>Color.Black</backcolor>
And in my class, i have the values of all the three nodes in a string. But how do i assign these string values to my HatchBrush( _ , _ , _ ) ??
I know that these string values cannot be directly assigned to HatchBrush, but i get the values from xml as string. How do i cast it ?
Dim style As String = "LargeConfetti" ' value from xml
Dim hs As HatchStyle = DirectCast([Enum].Parse(GetType(HatchStyle), style), HatchStyle)
Dim hb = New HatchBrush(hs, Color.LightGray, Color.DarkGray)
For the colors, same method but use Color enum instead of HatchStyle.

Replace part of string

I'm having difficulty with the following.
In VB.Net, I have the following line:
Dim intWidgetID As Integer = CType(Replace(strWidget, "portlet_", ""), Integer)
where strWidget = portlet_n
where n can be any whole number, i.e.
portlet_5
I am trying to convert this code to C#, but I keep getting errors, I currently have this:
intTabID = Convert.ToInt32(Strings.Replace(strTabID, "tab_group_", ""));
which I got using an online converter
But it doesn't like Strings
So my question is, how to I replace part of a string, so intTabID becomes 5 based on this example?
I've done a search for this, and found this link:
C# Replace part of a string
Can this not be done without regular expressions in c#, so basically, I'm trying to produce code as similar as possible to the original vb.net example code.
It should be like this strTabID.Replace("tab_group_", string.Empty);
int intTabID = 0;
string value = strTabID.Replace("tab_group_", string.Empty);
int.TryParse(value, out intTabID);
if (intTabID > 0)
{
}
And in your code i think you need to replace "tab_group_" with "portlet_"
Instead of Strings.Replace(strTabID, "tab_group_", ""), use strTabID.Replace("tab_group_", "").
This should work
int intWidgetID = int.Parse(strTabID.Replace("tab_group_",""));//Can also use TryParse
Their is no Strings class in Vb.Net so please use the string class instead http://msdn.microsoft.com/en-us/library/aa903372(v=vs.71).aspx
you can achieve it by this way
string strWidget = "portlet_n";
int intWidgetID = Convert.ToInt32(strWidget.Split('_')[1]);

Resources