2014-07-26 25 views
0

我有一個擴展方法定義爲:訪問擴展方法上的dbset亞型

public static class CurrentItemExtensions 
{ 
    static GPOPricingEntities ctx = new GPOPricingEntities(); 

    public static List<CurrentItem> Get(this DbSet<CurrentItem> item, int tierId, string contractId) 
    { 
     List<CurrentItem> items = ctx.Items.OfType<CurrentItem>().Where(x => x.TierId == tierId).ToList(); 

     if (items == null) 
     { 
      GPOPricing.AS400Models.ItemCollection collection = new GPOPricing.AS400Models.ItemCollection().Get(contractId); 

      foreach (var c in collection) 
      { 
       CurrentItem target = new CurrentItem(); 
       target.Price = c.DirectPriceEaches; 
       target.SKU = c.LongItemNbr; 
       target.Description = c.Description; 
       target.ProductLine = c.ProductLine; 

       items.Add(target); 
      } 
     } 
     else 
     { 
      foreach (var i in items) 
      { 
       GPOPricing.AS400Models.Item as400Item = new GPOPricing.AS400Models.ItemCollection().GetBySKU(i.SKU); 
       i.Description = as400Item.Description; 
       i.ProductLine = as400Item.ProductLine; 
      } 
     } 
     return items; 
    } 
} 

我在正在訪問的問題 - CURRENTITEM是項目的子類型。所以,我已經試過:

db.Items.Get (doesn't work) 

,我已經試過

db.Items.OfType<CurrentItem>().Get (doesn't work) 

有什麼建議?

回答

0

我發現我不得不使用基類型,併爲每種亞型的方法:

public static class CurrentItemExtensions 
{ 
    static GPOPricingEntities ctx = new GPOPricingEntities(); 

    public static List<CurrentItem> GetCurrentItems(this DbSet<Item> item, int tierId, string contractId) 
    { 
     List<CurrentItem> items = ctx.Items.OfType<CurrentItem>().Where(x => x.TierId == tierId).ToList(); 

     if (items.Count() == 0) 
     { 
      GPOPricing.AS400Models.ItemCollection collection = new GPOPricing.AS400Models.ItemCollection().Get(contractId); 

      foreach (var c in collection) 
      { 
       CurrentItem target = new CurrentItem(); 
       target.Price = c.DirectPriceEaches; 
       target.SKU = c.LongItemNbr; 

       items.Add(target); 
      } 
     } 
     else 
     { 
      foreach (var i in items) 
      { 
       GPOPricing.AS400Models.Item as400Item = new GPOPricing.AS400Models.ItemCollection().GetBySKU(i.SKU); 
      } 
     } 
     return items; 
    } 
}