【问题标题】:MVC C# web service join two tables and combine outputMVC C# Web 服务连接两个表并合并输出
【发布时间】:2017-06-18 06:09:57
【问题描述】:

我正在开发 iTunes/amazon/googlePlay 联盟计划,并希望将 AffiliateCode 添加到我在 Web 服务的 ExternalLinks 表中的 URL。这意味着网络服务首先使用 id_cd 从 ExternalLinks[Table] 收集不同的可用链接,然后需要循环访问 AffiliateProgram[Table] 以比较每个链接是否有可用的匹配联属计划。

ExternalLink[Table]
public int id_external_link { get; set; }
public int id_cd { get; set; }
public string Name { get; set; }
public string URL { get; set; }

AffiliateProgram[Table]
public int ID { get; set; }
public string AffiliateName { get; set; }
public string AffiliateSite { get; set; }
public string AffiliateCode { get; set; }
public Nullable<int> CodePosition { get; set; }

AffiliateProgram[Table]AffiliateName 和 ExternalLinks[Table]Name 始终相同。

这就是我设置它的方式,但我还没有让它工作,而且最近几天卡住了。

[Route("api/Cds/ExternalLinks/{id_cd}")]
    public async Task<List<ExternalLink>> GetAllExternalLinks(int id_cd)
    {
        List<ExternalLink> externallinks = await db.ExternalLink
            .Where(EL => EL.id_cd == id_cd)
            .ToListAsync();

        List<ExternalLink> ListAllExternalLinks = new List<ExternalLink>();
        for (int i = 0; i < 4; i++)
        {
           ExternalLink CDEL = new ExternalLink();

           if (CDEL.Name == "Itunes") {   

           var query = from EL in db.ExternalLink
                       join AP in db.AffiliateProgram on EL.Name equals AP.AffiliateName
                       select new
                       {
                        AffiliateName = AP.AffiliateName,
                        Name = EL.Name,
                        AffilateCode = AP.AffiliateCode,
                        URL = EL.URL
                       };

            URL = query.First().Name + "?mt=1&at=" + query.First().AffiliateCode + "&ct=" + id_cd;  

            }

            ListAllExternalLinks.Add(CDEL);

        }   
        ListAllExternalLinks.AddRange(externallinks);
        return ListAllExternalLinks;
    }

这会在 postmaster 中产生以下结果,有 4 个空结果:

[
{},
{},
{},
{},
{
"id_external_link": 1459,
"id_cd": 13376,
"name": "Itunes",
"url": "https://itunes.apple.com/...countingcrows"
},
{
"id_external_link": 1460,
"id_cd": 13376,
"name": "Amazon",
"url": "https://www.amazon.com/....countingcrows"
},
{
"id_external_link": 1461,
"id_cd": 13376,
"name": "GooglePlay",
"url": "https://play.google.com/...countingcrows"
}
]

【问题讨论】:

    标签: c# sql asp.net-mvc web-services model-view-controller


    【解决方案1】:

    您的查询是returning an anonymous object,您正在访问查询未返回的属性。 您可以更改查询以返回以下属性,然后访问它们:

            var query = from EL in db.ExternalLink
                        join AP in db.AffiliateProgram on EL.Name equals AP.AffiliateName
                        select new
                        {
                            AffiliateName = AP.AffiliateName
                            Name = EL.Name
                            AffiliateCode = AP.AffiliateCode
                        };
    

    【讨论】:

      【解决方案2】:

      您有许多语法错误,甚至会阻止此代码编译,因此一般来说,要准确找出哪里出错有点困难。至少先确保您的代码编译,然后如果您仍然没有得到所需的结果,我们可以帮助您。

      也就是说,看起来你让这件事变得比实际需要的困难得多。如果您希望能够连接这两个表,您应该在它们之间创建一个外键。如果特定链接属于特定联属网络营销计划,则实际创建该关系:

      public class ExternalLink
      {
          ...
      
          [ForeignKey("AffiliateProgram")]
          public int AffiliateProgramId { get; set; }
          public virtual AffiliateProgram AffiliateProgram { get; set; }
      }
      

      然后,您可以在查询并收工时简单地包含联属计划:

      var externalLinks = db.ExternalLinks
          .Include(m => m.AffiliateProgram)
          .Where(m => m.id_cd == id_cd)
          .ToListAsync();
      

      然后,当您遍历链接时:

      var affiliateCode = link.AffiliateProgram.AffiliateCode;
      

      超级简单。

      【讨论】:

      • 是的,我也知道了,但我无法更改数据库并因此添加外键,现在将添加可运行代码
      【解决方案3】:

      它开始工作了(实际上必须更改很多东西),感谢 S.Dav 解释如何连接其他数据库表并使用数据修改字符串。非常有用的一课。

          [Route("api/CDS/ExternalLink/{id_cd}")]
          public async Task<List<ExternalLink>> GetAllExternalLinks(int id_cd)
          {
      
              List<ExternalLink> ListAllExternalLinks = new List<ExternalLink>();
      
                  foreach (var item in await db.ExternalLink
                      .Where(EL => EL.id_cd == id_cd)
                      .ToListAsync())
                  {
      
                  ExternalLink CDEL = new ExternalLink();
      
                  CDEL.id_cd = item.id_cd;
                  CDEL.Name = item.Name;
      
                  if (CDEL.Name == "Itunes") {
      
                      var query = from EL in db.ExternalLink
                              join AP in db.AffiliateProgram on EL.Name equals AP.AffiliateName
                              select new
                              {
                              AffiliateName = AP.AffiliateName,
                              Name = EL.Name,
                              AffilateCode = AP.AffiliateCode,
                              URL = EL.URL
                              };
      
                      CDEL.URL = item.URL + query.First().Name + "?mt=1&at=" + query.First().AffilateCode + "&ct=" + id_cd;          
                  }
                  ListAllExternalLinks.Add(/CDEL);
              }
              return ListAllExternalLinks;
          }
      

      在这种情况下,会产生一个完美的网络服务,其中关联代码连接到 iTunes :-) 希望有一个可以实际找到关联代码本身的解决方案,但现在可以使用 :-)

      [
       {
      "id_cd": 13376,
      "name": "Itunes",
      "url": "https://itunes.apple.com/...../countingcrows?mt=1&at=1010lmNu&ct=13376"
       },
       {
      "id_cd": 13376,
      "name": "Amazon"
      "url": "https://www.amazon.com...../countingcrows"
       },
       {
      "id_cd": 13376,
      "name": "GooglePlay"
       "url": "https://play.google.com/....countingcrows?id=AEIKS4IbfNk&hl=en"
       }
      ]
      

      【讨论】:

        猜你喜欢
        • 2014-11-11
        • 1970-01-01
        • 2019-03-06
        • 1970-01-01
        • 1970-01-01
        • 2021-01-13
        • 1970-01-01
        • 2010-11-16
        • 2019-05-06
        相关资源
        最近更新 更多