views:

33

answers:

2

I have created a Area class using Linq-to-SQL.

Now I want to create a partial class of the same name so I can implement validation. Any help?

Error 1 Cannot implicitly convert type 'System.Data.Linq.Table<SeguimientoDocente.Area>' to 'System.Linq.IQueryable<SeguimientoDocente.Models.Area>' C:\Users\Sergio\documents\visual studio 2010\Projects\SeguimientoDocente\SeguimientoDocente\Models\AreaRepository.cs 14 20 SeguimientoDocente

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SeguimientoDocente.Models
{
    public class AreaRepository
    {
        private SeguimientoDataContext db = new SeguimientoDataContext();

        public IQueryable<Area> FindAllAreas()
        {
            return db.Areas;
        }

        public Area GetArea(int id)
        {
            return db.Areas.SingleOrDefault(a => a.ID == id);
        }

        public void Add(Area area)
        {
            db.Areas.InsertOnSubmit(area);
        }

        public void Delete(Area area)
        {
            db.Areas.DeleteOnSubmit(area);
        }

        public void Save()
        {
            db.SubmitChanges();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SeguimientoDocente.Models
{
    public partial class Area
    {

    }
}

Here's a screenshot. alt text

+1  A: 

The error message tells you that the problem is in line 14 of the AreaRepository.cs file. Specifically, you are trying to return db.Areas from a method whose return type is IQueryable<Area>, though db.Areas is in fact of type System.Data.Linq.Table.

Jay
I'm following this tutorial. Any suggestions?
Sergio Tapia
Note that this error only appears when I add the partial Area class. When there is no Area partial, the code compiles and words correctly.
Sergio Tapia
Actually the OP's question was not properly marked up. Normally Table<T> does indeed implement IQueryable<T> but in this case he's inadvertently defined a T2 that has the same simple name as T1.
Josh Einstein
@Josh Thank you. I considered that, but didn't see a namespace import in the class that uses the DataContext. I see now why that is, though.
Jay
+2  A: 

This is almost certainly because your partial class is not in the right namespace. Go into the .designer.cs file of the LINQ model, look for the generated Area class, and make sure the namespace you wrapped your partial class in matches.

EDIT

I just fixed the formatting in your question. The error message does in fact indicate that your partial class is in the wrong namespace.

Error 1 Cannot implicitly convert type 'System.Data.Linq.Table<SeguimientoDocente.Area>' to 'System.Linq.IQueryable<SeguimientoDocente.Models.Area>'

As you can see from the error above, you need to change the namespace your partial class is in to be SeguimientoDocente, not SeguimientoDocente.Models. As it stands now, they are two completely different incompatible types that happen to have the same simple name.

Josh Einstein