2017-01-03 42 views
0

嵌套的SELECT发生异常。如果你评论属性照片,生日和居住,那么一切正常。如何重写查询以使其工作?Mysql Linq已有一个与此连接关联的打开的DataReader,必须先关闭

var predicate = PredicateBuilder.True<Persons>(); 
    var query = this._entity.Persons.AsExpandable(); 

        #region 
        if (!String.IsNullOrEmpty(currentPerson.PersonName)) 
        { 
         predicate = predicate.And(i => i.PersonName.Contains(currentPerson.PersonName.Trim())); 
        } 

        if (!String.IsNullOrEmpty(currentPerson.PersonLastName)) 
        { 
         predicate = predicate.And(i => i.PersonLastName.Contains(currentPerson.PersonLastName.Trim())); 
        } 

        if (!String.IsNullOrEmpty(currentPerson.PersonPatronymic)) 
        { 
         predicate = predicate.And(i => i.PersonPatronymic.Contains(currentPerson.PersonPatronymic.Trim())); 
        } 
    ........... 

    var result = query.Where(predicate).AsEnumerable().Select(o => new POCO.PersonResult 
    { 
    Id  = (int)o.Id, 
    Photo  = o.persons_photos.Select(s => s.PersonFrontView).FirstOrDefault(), 
    FullName = String.Format("{0} {1} {2}", o.PersonLastName, o.PersonName, o.PersonPatronymic), 
    Birthday = o.persons_passport_data.Select(s => s.PersonBirthday).FirstOrDefault().ToString() 
    Residence = o.persons_registration 
            .Select(s => 
              String.Join(", ", ListModel.GetCountry(s.PersonCountryId), 
                   ListModel.GetRegion(s.PersonRegionId), 
                   ListModel.GetCity(s.PersonCityId))).FirstOrDefault() 
        }).ToList(); 

回答

2

看起来像MySQL连接器不支持MARS(多个活动结果集)和你的LINQ到对象查询(后AsEnumerable()呼叫)涉及懒加载(o.persons_photoso.persons_passport_datao.persons_registration导航性能),需要同时增设读者主数据读取器仍在执行。

最好是让整个查询通过移除AsEnumerable()调用执行作为单个的SQL查询,但由于投影使用不支持的方法,我想唯一的选择就是与ToList()更换AsEnumerable()通话,这将确保主要的数据读取器在导航属性延迟加载时完成。

你可以尝试的另一件事是渴望加载它们(假设你使用EF)加入一对夫妇Include电话:

var query = this._entity.Persons 
    .Include(o => o.persons_photos) 
    .Include(o => o.persons_passport_data) 
    .Include(o => o.persons_registration) 
    .AsExpandable(); 

// the rest (same as yours) ... 
+0

是多活动结果集的选项murash?否则,我也在考虑include选项。 –

+0

针对“MySQL MARS”的快速Google显示它们不受支持(因此无法像MSSQL提供程序那样打开) –

相关问题