0

我试图根据子查询计算布尔字段

  var dtfs = cntx.Set<Models.DocTypeField>()
                 .Include(dtf => dtf.Field)
                 .Where(dtf => dtf.DocTypeId == docTypeId)
                 .Select(dtf => new
            {
                DocTypeField = dtf,
                HasData = (cntx.Set<Models.DocumentData>()
                               .Any(dd => dd.DocTypeId == dtf.DocTypeId 
                                 && dd.DataValues.Any(ddv => ddv.FieldId == dtf.FieldId)))
            });

DocTypeField和之间没有导航属性(或可遍历的路径)DocumentData。当我运行上面的查询时,出现以下异常:

测试方法

Core.Sebring.DataAccess.Ef.Test.EF_DocTypeDALTest.EF_DocTypeDALTest_GetDocTypeIndexes
 threw exception: 
System.NotSupportedException: 
  LINQ to Entities does not recognize the method 
  'System.Data.Entity.DbSet`1[Core.Sebring.Models.DocumentData] 
     Set[DocumentData]()' method, and this method cannot be translated 
     into a store expression.

是否有完成上述查询的实体方式的 linq?如果可能的话,我宁愿不在DocTypeField和之间添加导航属性(或可遍历路径) DocumentData

*更新 1 * 作为解决方法,我做了

class FieldDocTypeField
    {
        public int DocTypeFieldId { get; set; }
        public int DocTypeId { get; set; }
        public int FieldDataType { get; set; }
        public int FieldId { get; set; }
        public byte[] LastChanged { get; set; }
        public bool Required { get; set; }
        public string FieldName { get; set; }
        public bool HasData { get; set; }
    }

var dtfs = cntx.DbContext.Database.SqlQuery<FieldDocTypeField>(@"select dtf.*,f.*,
            HasData = (CASE  WHEN EXISTS(Select DocumentDataValue.FieldId 
            from DocumentData 
            inner join DocumentDataValue on DocumentData.DocumentDataId=DocumentDataValue.DocumentDataId
            where DocumentData.DocTypeId = @DocTypeId AND dtf.FieldId = 1) THEN cast(1 as bit) ELSE cast(0 as bit) END)
            from DocTypeField dtf
            inner join Field f on dtf.FieldId = f.FieldId WHERE dtf.DocTypeId=@DocTypeId", new System.Data.SqlClient.SqlParameter("@DocTypeId", docTypeId));

            foreach (var dtf in dtfs)
            {
                docTypeFields.Add(new Models.DocTypeField
                {
                    DocTypeFieldId = dtf.DocTypeFieldId,
                    DocTypeId = dtf.DocTypeId,
                    FieldDataType = dtf.FieldDataType,
                    FieldId = dtf.FieldId,
                    LastChanged = dtf.LastChanged,
                    Required = dtf.Required,
                    FieldName = dtf.FieldName,
                    HasData = dtf.HasData
                });
            }

它不是很好,但它可以工作并完成相同的事情。我无法找到一种方法来使用 linq 到实体而不在实体之间添加导航属性来执行上述DocTypeField操作DocumentData

4

1 回答 1

0

您可以先将数据提取到内存中,然后再执行 2nd select

var dtfs = cntx.Set<Models.DocTypeField>()
    .Include(dtf => dtf.Field)
    .Where(dtf => dtf.DocTypeId == docTypeId)
    .ToList() // pull the data to memory and then the following select can execute successfully.
    .Select(dtf => new
    {
        DocTypeField = dtf,
        HasData = (cntx.Set<Models.DocumentData>()
            .Any(dd => dd.DocTypeId == dtf.DocTypeId
                       && dd.DataValues.Any(ddv => ddv.FieldId == dtf.FieldId)))
    });
于 2013-08-27T18:40:14.700 回答