我不应该将Linq To Sql实体表转换为其真实类型吗?

时间:2010-02-06 23:13:57

标签: c# linq-to-sql

当我从Page_Load方法调用“f_CategoriNewsGet”函数时,我得到null。但我可以看到tblNewsler对象在返回行之前已经满了对象。

protected void Page_Load(object sender, EventArgs e)
{
    var tblNewsler = News.f_CategoriNewsGet("ÇEVRE", 18); // return object is always
    Table<News> tblNewsler1 = tblNews;
    rptNatureNews.DataSource = tblNews != null ? tblNews.Take(6) : null;
    rptNatureNews.DataBind();
}

static public Table<News> f_CategoriNewsGet(string _sCategoriName, int _iTop)
{
    var tblNews = (from news in DAO.context.GetTable<News>()
                       join kategori in DAO.context.GetTable<NewsCategori>() on                                news.M_RefNewsCategoriId equals kategori.M_NewsCategoriId
                       where kategori.M_Adi.Equals(_sCategoriName)
                       orderby news.M_InsertDate descending
                       select news).Take(_iTop);
    return tblNews as Table<News>;
}

2 个答案:

答案 0 :(得分:1)

tblNews a Table<News>。它只是IQueryable<News>context.GetTable<News> Table<News>,但应用查询运算符会产生不同的类型 - 查询运算符的结果不再是Table<T>,而是a(可能是内部的)表示查询的LINQ to SQL类型。

答案 1 :(得分:0)

你有一个错误:

Table<News> tblNewsler1 = tblNews;

应该

Table<News> tblNewsler1 =  tblNewsler;