- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.Linq;
- using System.Data.Linq.Mapping;
- namespace ConsoleApplication1
- {
- [Table (Name="Customers")]
- public class Customer
- {
- private string _CustomerID;
- [Column(IsPrimaryKey=true , Storage="_CustomerID")]
- public string CustomerID
- {
- get
- { return this._CustomerID; }
- set
- {this._CustomerID=value;}
- }
- private string _City;
- [Column(Storage = "_City")]
- public string City
- {
- get
- { return this._City; }
- set
- { this._City = value; }
- }
- private EntitySet<order> _Orders;
- public Customer()
- {
- this._Orders=new EntitySet<order>();
- }
- [Association(Storage="_Orders",OtherKey="CustomerID")]
- public EntitySet<order> Orders
- {
- get {return this._Orders;}
- set{this._Orders.Assign(value);}
- }
- }
- [Table(Name = "Orders")]
- public class order
- {
- private int _OrderID = 0;
- private string _CustomerID;
- private EntityRef<Customer> _Customer;
- public order()
- {
- this._Customer = new EntityRef<Customer>();
- }
- [Column(Storage = "_OrderID", DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
- public int OrderID
- {
- get { return this._OrderID; }
- }
- [Column(Storage = "_CustomerID", DbType = "NChar(5)")]
- public string CustomerID
- {
- get { return this._CustomerID; }
- set { this._CustomerID = value; }
- }
- [Association(Storage = "_Customer", ThisKey = "CustomerID")]
- public Customer Customer
- {
- get { return this._Customer.Entity;}
- set { this._Customer.Entity = value; }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- DataContext db = new DataContext("server=(Local);database=northwind;uid=sa;pwd=sa");
- Table<Customer> Customers = db.GetTable<Customer>();
- //db.Log = Console.Out; //输入SQL语句
- IQueryable<Customer> custQuery =
- from cust in Customers
- where cust.Orders.Any()
- select cust;
- foreach (Customer custobj in custQuery)
- {
- Console.WriteLine("ID={0},Qty={1}", custobj.CustomerID, custobj.Orders.Count());
- }
- Console.WriteLine("请按任一键退出");
- Console.ReadKey();
- }
- }
- }
创建对象模型
最新推荐文章于 2024-02-07 22:09:49 发布