代码之家  ›  专栏  ›  技术社区  ›  Sergio Tapia

尝试创建分部类时出错

  •  2
  • Sergio Tapia  · 技术社区  · 15 年前

    我已经使用LINQ to SQL创建了一个区域类。

    现在,我想创建一个同名的分部类,以便实现验证。有什么帮助吗?

    < Buff行情>

    错误1无法隐式转换类型 ' system.data.linq.table<seguimientodocente.area> ' 到 ' system.linq.iqueryable<seguimientodocente.models.area> 'c:\users\sergio\documents\visual 工作室 2010年\项目\SeguiMientoDocente\SeguiMientoDocente\Models\AreaRepository.cs 14 20 SeguiMientoDocente

    < /块引用>
    使用系统;
    使用system.collections.generic;
    使用system.linq;
    使用system.web;
    
    命名空间SeguiMientoDocente.Models
    {
    公共类区域存储库
    {
    private seguimientodataContext db=new seguimientodataContext();
    
    公共查询区域(findallareas)
    {
    返回数据库区域;
    }
    
    公共区域GetArea(int id)
    {
    返回db.areas.singleOrDefault(a=>a.id==id);
    }
    
    公共空隙添加(面积)
    {
    db.面积.插入式提交(面积);
    }
    
    公共作废删除(面积)
    {
    db.areas.deleteonsubmit(区域);
    }
    
    公共void save()。
    {
    db.submitchanges();
    }
    }
    }
    
    使用系统;
    使用system.collections.generic;
    使用system.linq;
    使用system.web;
    
    命名空间SeguiMientoDocente.Models
    {
    公共部分类区域
    {
    
    }
    }
    < /代码> 
    
    

    这是截图。

    现在,我想创建一个同名的分部类,以便实现验证。有什么帮助吗?

    错误1无法隐式转换类型 ’System.Data.Linq.Table<SeguimientoDocente.Area>’ 到 ’System.Linq.IQueryable<SeguimientoDocente.Models.Area>'C:\users\sergio\documents\visual 工作室 2010年\项目\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
        {
    
        }
    }
    

    这是截图。

    2 回复  |  直到 15 年前
        1
  •  3
  •   Josh    15 年前

    这几乎是肯定的,因为您的分部类不在正确的命名空间中。进入linq模型的.designer.cs文件,查找生成的区域类,并确保将部分类包装在匹配的名称空间中。

    编辑

    我刚修正了你问题的格式。错误消息实际上表明您的分部类在错误的命名空间中。

    错误1无法隐式转换类型 ’ System.Data.Linq.Table<SeguimientoDocente.Area> ’ 到 ’ System.Linq.IQueryable<SeguimientoDocente.Models.Area>

    从上面的错误中可以看到,您需要将分部类所在的命名空间更改为seguimientodocente,而不是seguimientodocente.models。现在看来,它们是两种完全不同的不兼容类型,恰好具有相同的简单名称。

        2
  •  2
  •   Jay    15 年前

    错误消息告诉您问题出现在 AreaRepository.cs 文件。具体地说,你想回来 db.Areas 从返回类型为 IQueryable<Area> 虽然,虽然 DB区域 实际上是类型 System.Data.Linq.Table .