[c#]实体框架-转换 LINQ 方法链与 SelectMany 查询语法

发布时间: 2017/2/26 14:46:08
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想问以下的问题。 我建立一个查询将返回有关存储的电子邮件软件包的一些详细信息。

关系︰ 包 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:

那很容易。

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)
赞助商