The code I wrote is as below,
Default.aspx page
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %> <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:CheckBoxList ID="cboList" runat="server"> <asp:ListItem Text="One" Value="One"></asp:ListItem> <asp:ListItem Text="Two" Value="Two"></asp:ListItem> <asp:ListItem Text="Three" Value="Three"></asp:ListItem> </asp:CheckBoxList> </asp:Content>
and the code behind for the Default.aspx is as below,
namespace WebApplication2
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : Page
{
public enum MyEnum
{
One,
Two,
Three
};
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
IList<MyEnum> myEnums = new List<MyEnum>();
myEnums.Add(MyEnum.One);
myEnums.Add(MyEnum.Three);
UsingQueryExpression(myEnums);
UsingGenericLambdaExpression(myEnums);
UsingLambdaExpression(myEnums);
}
private void UsingQueryExpression(IList<MyEnum> myEnums)
{
var temp = (from listItem in cboList.Items.Cast<ListItem>()
join myEnum in myEnums on listItem.Value equals myEnum.ToString()
select (listItem.Selected = true)).ToList();
}
private void UsingGenericLambdaExpression(IList<MyEnum> myEnums)
{
var temp = cboList.Items.Cast<ListItem>()
.Join(
myEnums,
listItem => listItem.Value,
myEnum => myEnum.ToString(),
((listItem, myEnum) => listItem.Selected = true)
).ToList();
}
private void UsingLambdaExpression(IList<MyEnum> myEnums)
{
var temp = cboList.Items.Cast<ListItem>()
.Join<ListItem, MyEnum, string, ListItem>(
myEnums,
listItem => listItem.Value,
myEnum => myEnum.ToString(),
((myEnum, listItem) => myEnum))
.Select(selectedListItem => selectedListItem.Selected = true).ToList();
}
}
}
happy to learn from others.
Thanks
mohammad
No comments:
Post a Comment