创建对象模型

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.Linq;
  6. using System.Data.Linq.Mapping;
  7. namespace ConsoleApplication1
  8. {
  9.     [Table (Name="Customers")]
  10.     public class Customer
  11.     {
  12.         private string _CustomerID;
  13.         [Column(IsPrimaryKey=true , Storage="_CustomerID")]
  14.         public string CustomerID
  15.         {
  16.             get
  17.             { return this._CustomerID; }
  18.             set
  19.             {this._CustomerID=value;}
  20.         }
  21.         private string _City;
  22.         [Column(Storage = "_City")]
  23.         public string City
  24.         {
  25.             get
  26.             { return this._City; }
  27.             set
  28.             { this._City = value; }
  29.         }
  30.         private EntitySet<order> _Orders;
  31.         public Customer()
  32.         {
  33.             this._Orders=new EntitySet<order>();
  34.         }
  35.         [Association(Storage="_Orders",OtherKey="CustomerID")]
  36.         public EntitySet<order> Orders
  37.         {
  38.             get {return this._Orders;}
  39.             set{this._Orders.Assign(value);}
  40.         }
  41.     }
  42.     [Table(Name = "Orders")]
  43.     public class order
  44.     {
  45.         private int _OrderID = 0;
  46.         private string _CustomerID;
  47.         private EntityRef<Customer> _Customer;
  48.         public order()
  49.         {
  50.             this._Customer = new EntityRef<Customer>();
  51.         }
  52.         [Column(Storage = "_OrderID", DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
  53.         public int OrderID
  54.         {
  55.             get { return this._OrderID; }
  56.         }
  57.         [Column(Storage = "_CustomerID", DbType = "NChar(5)")]
  58.         public string CustomerID
  59.         {
  60.             get { return this._CustomerID; }
  61.             set { this._CustomerID = value; }
  62.         }
  63.         [Association(Storage = "_Customer", ThisKey = "CustomerID")]
  64.         public Customer Customer
  65.         {
  66.            get { return this._Customer.Entity;}
  67.             set { this._Customer.Entity = value; }
  68.         }
  69.     }
  70.     class Program
  71.     {
  72.         static void Main(string[] args)
  73.         {
  74.             DataContext db = new DataContext("server=(Local);database=northwind;uid=sa;pwd=sa");
  75.             Table<Customer> Customers = db.GetTable<Customer>();
  76.             //db.Log = Console.Out;   //输入SQL语句
  77.             IQueryable<Customer> custQuery =
  78.                 from cust in Customers
  79.                 where cust.Orders.Any()
  80.                 select cust;
  81.             foreach (Customer custobj in custQuery)
  82.             {
  83.                 Console.WriteLine("ID={0},Qty={1}", custobj.CustomerID, custobj.Orders.Count());
  84.             }
  85.             Console.WriteLine("请按任一键退出");
  86.             Console.ReadKey();
  87.         }
  88.     }
  89. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值