Linq To SQL 需要显式吗?

LinqToSQL needs explicity?

我正在尝试使用从 LinqToSQL 语句检索到的项目列表填充数据网格,但有些事情让我有些困惑。

当我明确地将 where 子句设置为等于硬编码整数时,列表 returned 没有任何问题。但是,当我使用包含相同整数的 属性 的对象时,列表是 returned 但数据网格不会填充。

正在 returned 的列表有,我假设私有和 public 属性。硬编码整数 return 列表包含所有属性,而具有 属性 return 列表的对象仅包含私有属性,而 public 属性说明 "Function evaluation is disabled because a previous function evaluation timed out"

Example:
     object.country
            _countryid
            _continentid
            _countryname
            CountryID
            ContinentID
            CountryName

这里有两个 LinqToSQL 语句(都在 returning 一个项目列表,但只有一个没有抛出错误):

工作 LinqToSQL 语句

protected void rgcountry_NeedDataSource(object sender, EventArgs e)
{
    List<db_entity.country> _clist;
    using (db_era.era_entities _ee = new db_era.era_entities())
    {
        _clist = (from a in _ee.countries where a.ContinentID == 4 select a).ToList();
    }
    if (_clist.Count > 0)
        this.rgcountry.DataSource = _clist;
    else
        this.rgcountry.DataSource = empty();
}

非工作 LinqToSQL 语句 -(已设置 continentselected 且 continentID 确实具有值)

protected void rgcountry_NeedDataSource(object sender, EventArgs e)
{
    List<db_entity.country> _clist;
    if (continentselected != null)
    {
        using (db_era.era_entities _ee = new db_era.era_entities())
        {
            _clist = (from a in _ee.countries where a.ContinentID == continentselected.ContinentID select a).ToList();
        }
        if (_clist.Count > 0)
            this.rgcountry.DataSource = _clist;
        else
            this.rgcountry.DataSource = empty();
    }
    else
        this.rgcountry.DataSource = empty();
}

我在这里错过了什么?或者这就是 LinqToSQL 的工作方式?

问题如下。 continentselected 使用不同的数据上下文检索。然后你通过说

为你的查询创建一个新的数据上下文
using (db_era.era_entities _ee = new db_era.era_entities())

and continentselected 不属于该上下文。 Linq to sql 不会对来自不同数据上下文的实体执行。对于您的查询,请使用您用于检索 continentselected 实体实例

的相同数据上下文