0

根据用户设计,我必须将四个查询合并在一起并将它们放在一个转发器中。

 var qryIssuer = from l in dbRRSP.LOA
                        join lrb in dbRRSP.LOAOrReferredBy on l.LOAOrReferredById equals lrb.LoaOrReferredById
                        join lat in dbRRSP.LOAAccessType on l.LOAAccessTypeId equals lat.LOAAccessTypeId
                        join iss in dbRRSP.Issuer on l.IssuerId equals iss.IssuerId
                        where
                            l.PersonId == personId
                        select new
                                   {
                                       LOAOrReferredByDescription = lrb.LoaOrReferredByDescription, 
                                       lat.LOAAccessTypeDescription,
                                       PersonType = "Issuer",
                                       LOAName = iss.CompanyName, 
                                       l.DateAdded
                                   };
          var qryEMD = from l in dbRRSP.LOA
                        join lrb in dbRRSP.LOAOrReferredBy on l.LOAOrReferredById equals lrb.LoaOrReferredById
                        join lat in dbRRSP.LOAAccessType on l.LOAAccessTypeId equals lat.LOAAccessTypeId
                        join emd in dbRRSP.Agent on l.AgentId equals emd.AgentId
                        where
                            l.PersonId == personId
                        select new 
                                    {
                                        LOAOrReferredByDescription = lrb.LoaOrReferredByDescription, 
                                        lat.LOAAccessTypeDescription,
                                        PersonType = "EMD",
                                        LOAName = emd.CompanyName, 
                                        l.DateAdded
                                    };

        var qryEmdRep = from l in dbRRSP.LOA
                        join lrb in dbRRSP.LOAOrReferredBy on l.LOAOrReferredById equals lrb.LoaOrReferredById
                        join lat in dbRRSP.LOAAccessType on l.LOAAccessTypeId equals lat.LOAAccessTypeId
                        join ar in dbRRSP.AgentRepresentative on l.EMDRepresentativeId equals ar.AgentRepresentativeId
                        join arp in dbRRSP.Person on ar.PersonId equals arp.PersonId
                        where
                            l.PersonId == personId
                        select new 
                                   {
                                       LOAOrReferredByDescription = lrb.LoaOrReferredByDescription, 
                                       lat.LOAAccessTypeDescription,
                                       PersonType = "EMD Rep",
                                       LOAName = arp.FirstName + ' ' + arp.LastName, l.DateAdded
                                   };

        var qryLOAPerson = from l in dbRRSP.LOA
                           join lrb in dbRRSP.LOAOrReferredBy on l.LOAOrReferredById equals lrb.LoaOrReferredById
                           join lat in dbRRSP.LOAAccessType on l.LOAAccessTypeId equals lat.LOAAccessTypeId
                           join lp in dbRRSP.LOAPerson on l.LOAPersonId equals lp.LOAPersonId
                           where
                               l.PersonId == personId
                           select new 
                                      {
                                          LOAOrReferredByDescription = lrb.LoaOrReferredByDescription, lat.LOAAccessTypeDescription,
                                          PersonType = "Person",
                                          LOAName = lp.LOAPersonName, 
                                          l.DateAdded
                                      };

这是四个查询。最棘手的部分是最后一个字段是日期时间,这给我带来了一些问题。我知道如何像这样将它们中的两个结合在一起:

var qryMultipleLOA = qryIssuer.Union(qryEMD).ToList().Select(loa => new ExtendedLOA
{
                                                                                  LOAOrReferredByDescription = loa.LOAOrReferredByDescription, 
                                                                                   LOAAccessTypeDescription = loa.LOAAccessTypeDescription, 
                                                                                   PersonType = loa.PersonType,
                                                                                    LOAName = loa.LOAName,

DateAdded = DateTime.Parse(loa.DateAdded.ToString()).ToString("MM/dd/yyyy")
 });

但是我不知道如何添加最后两个查询 - 首先我尝试将它包装在括号中并添加一个不起作用的 .Union,然后当我尝试用适当的 .ToLists 嵌套它们时,那​​没有也不行。

下面是将其绑定到转发器的代码。

 rptLOA.DataSource = qryMultipleLOA;
            rptLOA.DataBind();

建议将不胜感激。

4

1 回答 1

1

你尝试过类似的东西吗?

var qryMultipleLOA = qryIssuer.Union(qryEMD).Union(qryEmdRep).Union(qryLOAPerson).ToList();

如果您的查询的足迹是相同的,那么将它们相互链接应该不是问题。

编辑:

我还会推荐以下内容:

  1. 创建一个类来保存结果数据的实例。
  2. 与其创建从 Linq 生成的动态变量列表并希望它们都匹配,不如将 linq 结果汇集到一个列表中。这样您就可以立即判断您是否有类型不匹配。
  3. 一旦你有四个相同列表的列表,按照我上面的语法,联合将很快。

动态 Linq 列表可能很痛苦、笨拙,并且单个属性类型更改可能会在运行时而不是设计时抛出您的代码。如果您按照上述步骤操作,您的代码对您和其他人来说将更易于维护和清晰。

我希望这在某种程度上有所帮助。

于 2013-04-04T00:35:23.913 回答