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)]);
    }
  • 相关阅读:
    NOIP2010提高组乌龟棋 -SilverN
    NOIP2009 提高组T3 机器翻译 解题报告-S.B.S
    controller向layout传值
    解决Yii2中刷新网页时验证码不刷新的问题
    关联表 重命名
    定义action的允许访问方式
    js 拷贝clone
    是否包含指定字符串
    indexOf()定义和用法
    直接借鉴的 ids拼接
  • 原文地址:https://www.cnblogs.com/yangzhenping/p/3346012.html
Copyright © 2011-2022 走看看