实体框架 - 使用SelectMany转换LINQ方法链以查询语法

时间:2015-12-11 17:37:16

标签: c# sql-server entity-framework linq

我想问下一个问题。 我正在构建一个查询,它将返回有关存储的电子邮件包的一些详细信息。

关系: 包1 ------- N地址1 ------ N OutboundPackages

我想将testOK查询转换为表达式语法,特别是以下语句:

referenceGuids = pkg.PackageAddresses.SelectMany(s=>s.AddressOutboundPackages).Select(s=>s.Reference) 

完整示例:

var test = (from stamp in IssuedStamps
            join pkg in Packages.Include(i=>i.PackageAddresses) on stamp.Id equals pkg.IssuedStampId
            join addr in Addresses on pkg.Id equals addr.Package_Id
            join outb in OutboundPackages on addr.Id equals outb.Address_Id
            where stamp.PortalUserId == "f31c2582-f663-4f2e-a9f7-d41ba138f86c" && stamp.Used

 //I'd like to convert the following SelectMany LINQ method chain expression to query syntax
 //let referenceGuids = pkg.PackageAddresses.SelectMany(s=>s.AddressOutboundPackages).Select(s=>s.Reference)                            
            select new 
            {
                StampId = stamp.Id,
                RecipientEmail = pkg.PackageAddresses.FirstOrDefault(x => x.AddressType == 1).EmailAddress,
                SenderEmail = pkg.PackageAddresses.FirstOrDefault(x => x.AddressType == 0).EmailAddress,                                   
                DateSent = pkg.ReceivedDate,
                Remarks = stamp.Description,
                Ref = referenceGuids,
                Subject = pkg.Subject,
                IsMassMailing = pkg.ProcessingPackageId == null || pkg.ProcessingPackageId == string.Empty
             } 
             ).ToList();


var testOK = (from stamp in IssuedStamps
              join pkg in Packages.Include(i=>i.PackageAddresses.Select(s=>s.AddressOutboundPackages)).Include(i=>i.PackageAddresses) on stamp.Id equals pkg.IssuedStampId

              where stamp.PortalUserId == "f31c2582-f663-4f2e-a9f7-d41ba138f86c" && stamp.Used       

              let referenceGuids = pkg.PackageAddresses.SelectMany(s=>s.AddressOutboundPackages).Select(s=>s.Reference)        

              select new 
              {                                    
                  StampId = stamp.Id,
                  RecipientEmail = pkg.PackageAddresses.FirstOrDefault(x => x.AddressType == 1).EmailAddress,
                  SenderEmail = pkg.PackageAddresses.FirstOrDefault(x => x.AddressType == 0).EmailAddress,

                   DateSent = pkg.ReceivedDate,
                   Remarks = stamp.Description,
                   Ref = pkg.PackageAddresses.SelectMany(s => s.AddressOutboundPackages).Select(s => s.Reference),
                   Subject = pkg.Subject,
                   IsMassMailing = pkg.ProcessingPackageId == null || pkg.ProcessingPackageId == string.Empty

               } 
               ).ToList();

1 个答案:

答案 0 :(得分:1)

这很容易。

referenceGuids = pkg.PackageAddresses
                 .SelectMany(s => s.AddressOutboundPackages)
                 .Select(s => s.Reference)

转到

referenceGuids = (from pa in pkg.PackageAddresses
                  from aop in pa.AddressOutboundPackages
                  select aop.Reference)