Fun with LINQ - Find the Longest and Shortest Type Name in .NET 3.5 using LINQ

| Posted in

0

While browsing through the forums a couple of days ago, I came across this query where the user wanted to list down the types in .NET 3.5 in the order of the length of their names. He also wanted to find out the total number of types in .NET 3.5 and 2.0. Here’s my attempt of how to do so using LINQ. If you know of a better way or find any discrepancy in the results displayed, please do let me know.
I have created a Console Application in Visual Studio 2008. Let’s take the problem one at a time.
Before I begin, here’s a disclaimer. The results may vary on your machine. Since we are referring to the GetExportedTypes() which returns type visible outside the assembly, you can get different results by adding new references (Right click project > Add Reference ) or by changing the access modifiers of the types. Having said that, let’s get started:
Listing down the public types in .NET 3.5
We can use reflection and a simple LINQ query to get the Types in .NET 3.5 as shown below:
C#
static void Main(string[] args)
{
var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
from aType in assembly.GetExportedTypes()
select aType;
var aList = assemb
.Where(x => x.Assembly.FullName.Contains("Version=3.5.0.0"))
foreach (var a in aList)
{
Console.WriteLine("{0}", a.Name);
}
Console.ReadLine();
}
VB.NET
Sub Main()
Dim assemb = _
From assembly In AppDomain.CurrentDomain.GetAssemblies(),aType In assembly.GetExportedTypes() _
Select aType
Dim aList = assemb.Where(Function(x) x.Assembly.FullName.Contains("Version=3.5.0.0"))
For Each a In aList
Console.WriteLine("{0}", a.Name)
Next
Console.ReadLine()
End Sub
Note: If you want to Fully qualify the type name, use v.FullName instead of v.Name
Output:
List Assemblies
Listing down the types in the order of the length of their names
An OrderBy Descending would do the trick here as shown below:
C#
static void Main(string[] args)
{
var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
from aType in assembly.GetExportedTypes()
select aType;
var aList = assemb
.Where(x => x.Assembly.FullName.Contains("Version=3.5.0.0"))
.OrderByDescending(x => x.Name.Length);
foreach (var a in aList)
{
Console.WriteLine("Length: {0} {1}", a.Name.Length, a.Name);
}
Console.ReadLine();
}
VB.NET
Sub Main(ByVal args() As String)
Dim assemb = _
From assembly In AppDomain.CurrentDomain.GetAssemblies(),aType In assembly.GetExportedTypes() _
Select aType
Dim aList = assemb.Where(Function(x) x.Assembly.FullName.Contains("Version=3.5.0.0")).OrderByDescending(Function(x) x.Name.Length)
For Each a In aList
Console.WriteLine("Length: {0} {1}", a.Name.Length, a.Name)
Next a
Console.ReadLine()
End Sub
Output:
Longest Type
As seen above, the Longest Type Name as in this example is 38 characters long ‘ManifestSignatureInformationCollection’
Shortest Type
and the Shortest Type Name is 3 characters long ‘Aes’
Total types in .NET 2.0 and .NET 3.5
In order to find the total types in .NET 2.0 and .NET 3.5, we will use the GroupBy clause as shown here:
C#
static void Main(string[] args)
{
var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
from aType in assembly.GetExportedTypes()
select aType;
var vers = assemb.Select(
m => m.Assembly.FullName.Split(",".ToCharArray())[1])
.GroupBy(x => x)
.Select(x => new { VerName = x.Key, Count = x.Count() });
foreach (var ver in vers)
{
Console.WriteLine(".NET {0} has {1} types\n",ver.VerName, ver.Count);
}
Console.ReadLine();
}
VB.NET
Sub Main(ByVal args() As String)
Dim assemb = _
From assembly In AppDomain.CurrentDomain.GetAssemblies(), aType In assembly.GetExportedTypes() _
Select aType
Dim vers = assemb.Select(Function(m) m.Assembly.FullName.Split(",".ToCharArray())(1)) _
.GroupBy(Function(x) x) _
.Select(Function(x) New With {Key .VerName = x.Key, Key .Count = x.Count()})
For Each ver In vers
Console.WriteLine(".NET {0} has {1} types" & Constants.vbLf, ver.VerName, ver.Count)
Next ver
Console.ReadLine()
End Sub
Output:
C#
Csharp Output
VB.NET
VB Output

Check/Uncheck all items in a CheckBoxList using ASP.NET and Javascript

| Posted in

0

The CheckBoxList control in ASP.NET 2.0 provides a group of checkboxes that can be dynamically generated by binding it to a data source. In this article, we will explore how to use a Checkboxlist and use ASP.NET and Javascript to select or unselect all your checkboxes in your CheckBoxList.
Using ASP.NET
Step 1: Open Visual Studio. Create a new website called ‘CheckUncheckAll’. Drag and drop a CheckBoxList control to the page. Rename it to ‘cblMulti’.
Step 2: Once the CheckBoxList is added to the page, a smart tag appears which allows you to specify a datasource or add items manually to the CheckBoxList. Click on the ‘Edit Items’ to open the ListItem Collection Editor and add items as shown in the figure below :
Note: If you do not want to use the editor and want to add items to the CheckBoxList programmatically, use the following code at the Page load event :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
cblMulti.Items.Add(new ListItem("Items 1", "Items 1"));
cblMulti.Items.Add(new ListItem("Items 2", "Items 2"));
cblMulti.Items.Add(new ListItem("Items 3", "Items 3"));
}
}
However, you can also bind the checkbox list to a datareader as show below :
// Assuming that GetListItems returns a list of customerid and customername items in a sqldatareader
SqlDataReader dr = GetListItems();
cblMulti.DataSource = dr;
cblMulti.DataValueField = "cust_no";
cblMulti.DataTextField = "cust_name";
cblMulti.DataBind();
To keep things simple, we will be adding items programatically by using the cblMulti.Items.Add(new ListItem(text,value)) in our example.
Step 3: Drag and drop two LinkButtons from the toolbox to the page and rename them as ‘lbAll’ and ‘lbNone’ respectively.
The code will look like this :
<asp:LinkButton ID="lbAll" runat="server">Select Allasp:LinkButton>
<asp:LinkButton ID="lbNone" runat="server">Select Noneasp:LinkButton>
Step 4: Double click the two ‘Link Button’ to generate the click events for them. In the click events of the two Link Buttons(), add the following code:
// Check All
protected void lbAll_Click(object sender, EventArgs e)
{
foreach (ListItem li in cblMulti.Items)
{
li.Selected = true;
}
}
// Uncheck All
protected void lbNone_Click(object sender, EventArgs e)
{
foreach (ListItem li in cblMulti.Items)
{
li.Selected = false;
}
}
Step 5 : Run the application and test it. Click on the ‘Select All’ button to select all the checkboxes at once. Similarly click on the ‘Select None’ button to deselect all the checkboxes at once.
So that was simple.
Using Javascript
We can do the same using javascript. However for our javascript example, let us add some complexity into our examples. We will take a master page example, and will now add two checkboxlist instead of one to our page. Follow these steps :
Step 1: Open Visual Studio. Create a new website called ‘CheckUncheckJS’. Drag and drop two CheckBoxList controls to the page. Rename it to ‘cbl1’ and ‘cbl2’.
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:CheckBoxList ID="cbl1" runat="server">
asp:CheckBoxList><br />
<asp:CheckBoxList ID="cbl2" runat="server">
asp:CheckBoxList>
asp:Content>
Step 2: Populate the two checkboxlist controls in the Page_Load()
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
cbl1.Items.Add(new ListItem("Items 1", "Items 1"));
cbl1.Items.Add(new ListItem("Items 2", "Items 2"));
cbl1.Items.Add(new ListItem("Items 3", "Items 3"));
cbl2.Items.Add(new ListItem("Items 4", "Items 4"));
cbl2.Items.Add(new ListItem("Items 5", "Items 5"));
cbl2.Items.Add(new ListItem("Items 6", "Items 6"));
}
}
Step 3: It’s time to add the SelectAll/None feature to our page. However before we go ahead, we need to understand how to pass the controls kept in content pages to the javascript function. When the page is rendered in the browser, the content and master pages get merged into a single page. This means that the IDs for the controls get renamed. ASP.NET renames the IDs to prevent naming conflicts. Now to handle the renamed controls, ASP.NET provides the ‘Control.ClientID’ and ‘Control.UniqueID’ to get the renamed ID’s.
Having understood this, let us now add some controls to our page and then pass the respective controls to the javascript using the Control.ClientID.
The prototype of our javascript function will be the following :
function CheckBoxListSelect(, )
{
// Our code will come here
}
Step 4: Add two anchor tags beneath each checkboxlist controls in your source. Remember that we are using two checkboxlist controls.
<asp:CheckBoxList ID="cbl1" runat="server">asp:CheckBoxList><br />
Select <a id="A1" href="#">Alla> | <a id="A2" href="#">Nonea>
<asp:CheckBoxList ID="cbl2" runat="server">asp:CheckBoxList><br />
Select <a id="A3" href="#">Alla> | <a id="A4" href="#">Nonea>
Step 5: Add the onclick event to the anchor tags as shown below. In this event, we will call the javascript function and pass the respective checkboxlist controls and the state which will describe ‘true’ or ‘false’ for each checkbox
The code at the end will look like this :
<asp:CheckBoxList ID="cbl1" runat="server">asp:CheckBoxList><br />
Select <a id="A1" href="#" onclick="javascript: CheckBoxListSelect ('<%= cbl1.ClientID %>',true)">Alla>
| <a id="A2" href="#" onclick="javascript: CheckBoxListSelect ('<%= cbl1.ClientID %>',false)">Nonea>
<br />
<br />
<asp:CheckBoxList ID="cbl2" runat="server">
asp:CheckBoxList>
Select <a id="A3" href="#" onclick="javascript: CheckBoxListSelect ('<%= cbl2.ClientID %>',true)">Alla>
| <a id="A4" href="#" onclick="javascript: CheckBoxListSelect ('<%= cbl2.ClientID %>',false)">Nonea>
Step 6: Let us add the javascript function to the content page now.
For this add the