我正在使用iTunes / amazon / googlePlay联盟计划,并希望将AffiliateCode添加到我在Web服务中的ExternalLinks表中的URL。这意味着web服务首先使用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 [表] AffiliateName和ExternalLinks [表]名称始终相同。
这就是我设置它的方式,但我还没有让它工作,并且在最后几天卡住了。
[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"
}
]
答案 0 :(得分:3)
您的查询是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
};
答案 1 :(得分:1)
您有许多语法错误会阻止此代码进行编译,因此通常很难弄清楚您出错的确切位置。至少要确保您的代码首先编译,然后如果您仍未获得所需的结果,我们可以为您提供帮助。
那就是说,看起来你要比现在更困难。如果您希望能够加入这两个表,则应在它们之间创建一个外键。如果特定链接属于特定联盟计划,则实际创建该关系:
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;
超级简单。
答案 2 :(得分:0)
好了它的工作(实际上必须改变很多东西),谢谢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"
}
]