zoukankan      html  css  js  c++  java
  • C#3.0 LINQ 操作符

    Table 类:

    public class DemoDataContext : DataContext
    {
      public DemoDataContext (string cxString) : base (cxString) {}
    
      public Table<Customer> Customers { get { return GetTable<Customer>(); } }
      public Table<Purchase> Purchases { get { return GetTable<Purchase>(); } }
    }
    
    [Table] public class Customer
    {
      [Column(IsPrimaryKey=true)]  public int ID;
      [Column]                     public string Name;
    
      [Association (OtherKey="CustomerID")]
      public EntitySet<Purchase> Purchases = new EntitySet<Purchase>();
    }
    
    [Table] public class Purchase
    {
        [Column(IsPrimaryKey=true)]  public int ID;
        [Column]                     public int CustomerID;
        [Column]                     public string Description;
        [Column]                     public decimal Price;
        [Column]                     public DateTime Date;
    
      EntityRef<Customer> custRef;
    
      [Association (Storage="custRef",ThisKey="CustomerID",IsForeignKey=true)]
      public Customer Customer
      {
        get { return custRef.Entity; } set { custRef.Entity = value; }
      }
    }

    使用SQL来创建表:

    create table Customer
    (
      ID int not null primary key,
      Name varchar(30) not null
    )
    create table Purchase
    (
      ID int not null primary key,
      CustomerID int not null references Customer (ID),
      Description varchar(30) not null,
      Price decimal not null
    )

    Join和GroupJoin实现:

    public static IEnumerable <TResult> Join
                                        <TOuter,TInner,TKey,TResult> (
      this IEnumerable <TOuter>     outer,
      IEnumerable <TInner>          inner,
      Func <TOuter,TKey>            outerKeySelector,
      Func <TInner,TKey>            innerKeySelector,
      Func <TOuter,TInner,TResult>  resultSelector)
    {
      ILookup <TKey, TInner> lookup = inner.ToLookup (innerKeySelector);
      return
        from outerItem in outer
        from innerItem in lookup [outerKeySelector (outerItem)]
        select resultSelector (outerItem, innerItem);
    }
    
    public static IEnumerable <TResult> GroupJoin
                                        <TOuter,TInner,TKey,TResult> (
      this IEnumerable <TOuter>     outer,
      IEnumerable <TInner>          inner,
      Func <TOuter,TKey>            outerKeySelector,
      Func <TInner,TKey>            innerKeySelector,
      Func <TOuter,IEnumerable<TInner>,TResult>  resultSelector)
    {
      ILookup <TKey, TInner> lookup = inner.ToLookup (innerKeySelector);
      return
        from outerItem in outer
        select resultSelector
         (outerItem, lookup [outerKeySelector (outerItem)]);
    }
  • 相关阅读:
    Linux下查找包含BOM头的文件和清除BOM头命令 2014-08-16 12:30:50
    ecshop lang用法
    php常用Stream函数集介绍
    php实现多任务并发探讨
    ThinkPHP 3.2 版本升级了哪些内容
    免费手机号码归属地API查询接口和PHP使用实例分享
    [SHELL进阶] (转)最牛B的 Linux Shell 命令 (四)
    [SHELL进阶] (转)最牛B的 Linux Shell 命令 (三)
    [SHELL进阶] (转)最牛B的 Linux Shell 命令 (二)
    [SHELL实例] (转)最牛B的 Linux Shell 命令 (一)
  • 原文地址:https://www.cnblogs.com/yangzhenping/p/3346012.html
Copyright © 2011-2022 走看看