ThomasAJ
ThomasAJ

Reputation: 721

Linq to SQL type mismatch

I have a type mismatch error when reading the contents of table 'ImageHighlight'.

In designer.cs the table is:

public System.Data.Linq.Table<ImageHighlight>ImageHighlights
{
    get
    {
        return this.GetTable<ImageHighlight>();
    }
}

In my code I am trying to cache a small table in method LoadStaticCache() at ApplicationStart so I can access its contents later via GetHighlightImages().

public class StaticCache
{

    private static ImageHighlight _images = null;

    public static void LoadStaticCache()
    {
        // Get images - cache using a static member variable
        using (var datacontext = new MHRDataContext())
        {
            _images = datacontext.ImageHighlights;
        }
    }
     public static ImageHighlight GetHighlightImages()
    {
        return _images;
    }
}

At code line _images = datacontext.ImageHighlights; I get error

Cannot implicitly convert type System.Data.Linq.Table<HolidayRentals.Core.Domain.LinqToSql.ImageHighlight> to HolidayRentals.Core.Domain.LinqToSql.ImageHighlight

They are both the same type.

Upvotes: 1

Views: 139

Answers (1)

Hamid Pourjam
Hamid Pourjam

Reputation: 20744

datacontext.ImageHighlights is a Table which is an IQueryable of ImageHighlight. _imagess type is ImageHighlight. You can not convert these types to each other.

Since you want some caching mechanism and _images indicates that it should contain multiple instance of images then you should change the type of _images.

Change your code to this:

public class StaticCache
{

    private static List<ImageHighlight> _images = null;

    public static void LoadStaticCache()
    {
        // Get images - cache using a static member variable
        using (var datacontext = new MHRDataContext())
        {
            _images = datacontext.ImageHighlights.ToList();
        }
    }
    public static List<ImageHighlight> GetHighlightImages()
    {
        return _images;
    }
}

Upvotes: 1

Related Questions