tags:

views:

78

answers:

1

I have my objects like this.

public class BaseTable
{
    public int ID               { get; set; }
    public int ParentID         { get; set; }
    public string Description   { get; set; }
    public bool IsActive        { get; set; }    

}

public class CarFuelType : BaseTable
{

}

and a test class

public class Test
{
    public IList<CarFuelType> GetAllActiveFuelTypes()
    {
        var result = GetAllActiveData<CarFuelType>(LookUpTableConstants.CarFuelType);

        return result.Cast<CarFuelType>().ToList(); 
    }


    private IList<T> GetAllActiveData<T>(int filter)
    {
        var result = from c in _lookUpTableValuesRepository.GetAllRowsWhere(l => l.ParentID == filter && l.IsActive==true)
                     select new BaseTable{ ID = c.ID, Description = c.Description, ParentID = c.ParentID };
        return result.ToList() as IList<T>;
    }

}

but I am getting null result returned from GetAllActiveData method. Is there anyway I convert IList from one type to another.

+6  A: 

You've got a problem here, because you're not actually creating any instances of the derived class. You need to actually create instances of CarFuelType rather than BaseTable. Once you've done that, you won't need any casting. You can do that in this way - at least for LINQ to Objects:

private IList<T> GetAllActiveData<T>(int filter) where T : BaseTable, new()
{
    var result = from c in _lookUpTableValuesRepository
                               .GetAllRowsWhere(l => l.ParentID == filter 
                                                && l.IsActive==true)
                 select new T() { ID = c.ID, 
                                  Description = c.Description,
                                  ParentID = c.ParentID };
    return result.ToList();
}

I doubt this will work for LINQ to SQL or the Entity Framework though... you'd probably have to create instances of BaseTable in the LINQ to SQL query, then convert them in memory, e.g.

    var baseQuery = from c in _lookUpTableValuesRepository
                               .GetAllRowsWhere(l => l.ParentID == filter 
                                                && l.IsActive==true)
                 select new BaseTable { ID = c.ID, 
                                        Description = c.Description,
                                        ParentID = c.ParentID };

    var converted = baseQuery.AsEnumerable()
                             .Select(b => new T() { ID = b.ID, 
                                                    Description = b.Description,
                                                    ParentID = b.ParentID };

That may lose the "entity-ness" however - you may have problems using those objects for updates etc.

Jon Skeet
it shows me the error Unable to cast object of type 'AutoSales.DataAccess.Domain.BaseTable' to type 'AutoSales.DataAccess.DataAccess.CarFuelType'.
Parminder
@parminder: I've just edited, having looked at it again.
Jon Skeet
worked like charm. thanks a lot. are you the guy who wrote the book C# in depth. Thanks
Parminder
@parminder: Yup. Glad it worked for you :)
Jon Skeet
that book is just fantastic.
Parminder
@parminder: You're very kind. I'm hard at work on the second edition at the moment...
Jon Skeet