foreach遍历linq所有数据
linq 能查询的数据
对象。比如集合、数组、字符串等等。
关系。分为 linq to dataset 和 linq to sql,前者用于 dataset 查询,后者用于 sql server 数据库教程查询(相信今后还会支持其他数据库)。
xml。xml 文件、xml 对象等。
linq 优点
不论查什么数据,都是统一的结构。
功能比其他查询方式更强。
可以在编译时进行语法检查,不像 sql 字符串,只能在运行时发现其是否有语法错误。
可以在编译时进行类型检查。
开发时有 intellisense 支持。
调试方便。
linq 环境
linq 要在 .net framework 3.5 下使用,其支持的语言有:c# 3.0、vb9。所以在 windows 2000 下无法使用 linq,因为 windows 2000 下无法安装 .net framework 3.5
using system;
using system.collections.generic;
using system.linq;
using system.text;public class mainclass {
public static void main() {
listcustomers = getcustomerlist();
var wacustomers =
from c in customers
where c.region == "r1"
select c;
foreach (var customer in wacustomers) {
console.writeline("customer {0}: {1}", customer.customerid, customer.companyname);
foreach (var order in customer.orders) {
console.writeline(" order {0}: {1}", order.id, order.orderdate);
}
}
}
static listgetcustomerlist() {
listemptree = new list ();
emptree.add(new product { productname = "a", category = "o", unitprice = 12, unitsinstock = 5, total = 36, orderdate = new datetime(2005, 1, 1), id = 1 });
emptree.add(new product { productname = "b", category = "o", unitprice = 2, unitsinstock = 4, total = 35, orderdate = new datetime(2005, 1, 1), id = 1 });
emptree.add(new product { productname = "c", category = "o", unitprice = 112, unitsinstock = 3, total = 34, orderdate = new datetime(2005, 1, 1), id = 1 });
emptree.add(new product { productname = "d", category = "o", unitprice = 112, unitsinstock = 0, total = 33, orderdate = new datetime(2005, 1, 1), id = 1 });
emptree.add(new product { productname = "e", category = "o", unitprice = 1112, unitsinstock = 2, total = 32, orderdate = new datetime(2005, 1, 1), id = 1 });
emptree.add(new product { productname = "f", category = "o", unitprice = 11112, unitsinstock = 0, total = 31, orderdate = new datetime(2005, 1, 1), id = 1 });list
l = new list ();
l.add(new customer { companyname = "a", region = "r1", unitsinstock = 1, orders = emptree, customerid =0});
l.add(new customer { companyname = "b", region = "r2", unitsinstock = 2, orders = emptree, customerid = 1 });
l.add(new customer { companyname = "c", region = "r3", unitsinstock = 3, orders = emptree, customerid = 2 });
l.add(new customer { companyname = "d", region = "r4", unitsinstock = 4, orders = emptree, customerid = 3 });
l.add(new customer { companyname = "e", region = "r5", unitsinstock = 5, orders = emptree, customerid = 4 });
return l;
}
}class customer : icomparable
{
public string companyname { get; set; }
public string region { get; set; }
public listorders { get; set; }
public int unitsinstock { get; set; }
public int customerid { get; set; }public override string tostring() {
return string.format("id: {0}, name: {1}, region: {3}", this.customerid, this.companyname, this.region);
}
int icomparable.compareto(customer other) {
if (other == null)
return 1;if (this.customerid > other.customerid)
return 1;if (this.customerid < other.customerid)
return -1;return 0;
}
}
class product : icomparable{
public string productname { get; set; }
public string category { get; set; }
public int unitprice { get; set; }
public int unitsinstock { get; set; }
public int total { get; set; }
public datetime orderdate { get; set; }
public int id { get; set; }public override string tostring() {
return string.format("id: {0}, name: {1} , category: {3}", this.id, this.productname, this.category);
}
int icomparable.compareto(product other) {
if (other == null)
return 1;
if (this.id > other.id)
return 1;if (this.id < other.id)
return -1;return 0;
}
}
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级