linq 的用法小记-匿名类

这个代码里面用到了匿名类。
匿名类的用法, 感觉有点像JS里面的JSON了。但事实上,还是有区别的。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LinqDemo
{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            InitializeComponent();
        }


        
private void btForeach_Click(object sender, EventArgs e)
        
{
            List
<Account> listAccount = CreateList();

            var accounts 
= from account in listAccount
                           orderby account.UserName
                           select 
new { account.UserName, account.Pwd };

            
foreach (var account in accounts)
            
{
                lvForeach.Items.Add(account.Pwd.ToString());
            }

        }

        List
<Account> CreateList()
        
{
            List
<Account> list = new List<Account>();
            list.Add(
new Account("account4""Paccount4"));
            list.Add(
new Account("account1""Paccount1"));
            list.Add(
new Account("account2""Paccount2"));
            list.Add(
new Account("account3""Paccount3"));
            
return list;
        }

    }


    
public class Account
    
{
        
public string UserName
        
{
            
get;
            
set;
        }

        
public string Pwd
        
{
            
get;
            
set;
        }

        
public Account(string username,string pwd)
        
{
            
this.UserName = username;
            
this.Pwd = pwd;
        }

    }

}


Tag标签: linq

posted on 2008-07-07 18:06 房客 阅读(70) 评论(0)  编辑 收藏 所属分类: CodeHide


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
"五向定位"职业成长路线公开课(上海、南京、大连)
Google站内搜索


相关链接:
 




导航

<2008年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

统计

与我联系

搜索

 

常用链接

留言簿(4)

我参与的团队

我的标签

随笔分类

随笔档案

Blogs

WebSite

最新随笔

积分与排名

最新评论

  • 1. re: Switch TOR's Identity
  • 您好,我最近也在研究TOR里的一些东西,有个问题想问你,在TOR的源代码中有好多常变量名字,搞了半天也搞不懂是什么意思,比如newnym,rephist,sighup等,如果你有时间并且乐于助人的话,...
  • --迷彩使者

阅读排行榜