我试图用从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语句-(设置了连续选择且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的工作方式?
发布于 2015-07-22 19:16:09
问题如下。使用不同的数据上下文检索continentselected。并通过以下方式为查询创建新的数据上下文:
using (db_era.era_entities _ee = new db_era.era_entities())
而continentselected不属于这个上下文。Linq到sql不会对来自不同数据上下文的实体执行。对于查询,使用与检索continentselected实体实例相同的数据上下文。
https://stackoverflow.com/questions/31571227
复制相似问题