0

I have an extension method defined as:

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;
    }
}

The problem I'm having is accessing it - CurrentItem is a subtype of Item. So I've tried:

 db.Items.Get (doesn't work)

and I have tried

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

Any suggestions?

1 Answers1

0

I found that I had to use the base type and create a method for each subtype:

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;
    }
}