【问题标题】:LinqToSQL needs explicity?Linq SQL 需要明确吗?
【发布时间】:2015-07-22 18:30:15
【问题描述】:

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

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

返回的列表有,我假设私有和公共属性。硬编码的整数返回列表包含所有属性,而具有属性返回列表的对象仅包含私有属性,而公共属性声明“函数评估已禁用,因为先前的函数评估超时”

Example:
     object.country
            _countryid
            _continentid
            _countryname
            CountryID
            ContinentID
            CountryName

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

工作 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 的工作方式?

【问题讨论】:

  • ContinentID getter 中有什么逻辑吗?我也没有看到对数据绑定的调用...
  • 您确定continentselected.ContinentID 实际上具有有效值吗?因为它是整数,所以默认为 0。你确定它设置为 4 吗?
  • @Stephen Panzer:对于 Telerik RadGrid,您不需要数据绑定。 ContinentID getter 到底是什么意思?
  • @neo:是的,我是肯定的continentselected。ContinentID 确实有一个有效值。范围从 1 到 7,因为我们的数据库中有 7 个大陆。 And the continentselected is set when another datagrid item is selected.这一点我很确定。
  • @mattgcon:我使用 devexpress 网格进行此类查询没有问题。大陆选择和大陆选择.ContinentID的类型是什么?

标签: c# asp.net linq-to-sql


【解决方案1】:

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

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

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

相同的数据上下文

【讨论】:

  • 这是不可能的,因为 continentselected 来自另一个在填充之前填充的数据网格,并且两个网格都位于单独的用户控件中。我什至提取了continentID并将其放入一个新变量(即字节conID)中,基本上只是将一个字节传递给a.ContinentID,它仍然会导致同样的问题。
  • 当您说“continentselected 来自另一个数据网格”时,您到底是什么意思?网格可以给你它的行数据,网格通常返回类型对象。
  • 国家/地区网格位于带有另一个网格(大陆)的网页上,该网格最初在加载页面时加载。然后用户将选择然后加载国家网格的大陆。 Telerik 网格有点奇怪,因为它们返回数据键值而不是网格中的对象类型。
  • 您不使用该 datakey 值来检索实际的行数据吗?
  • 反之不行,当行被选中时,它会通过griditem,获取值的方法是指定你想要获取的数据键。
猜你喜欢
  • 1970-01-01
  • 2013-11-25
  • 1970-01-01
  • 2021-11-10
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
相关资源
最近更新 更多