简体   繁体   English

MVC C#Web服务连接两个表并组合输出

[英]MVC C# web service join two tables and combine output

I am working on iTunes/amazon/googlePlay affiliate program and want to add the AffiliateCode to the URL i have in my ExternalLinks Table within the web service. 我正在使用iTunes / amazon / googlePlay联盟计划,并希望将AffiliateCode添加到我在Web服务中的ExternalLinks表中的URL。 This means that the webservice first collects with the id_cd the different available links from the ExternalLinks[Table] and after is needs to loop over the AffiliateProgram[Table] for each of them to compare if there is a matching affiliate program available. 这意味着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; }

The AffiliateProgram[Table]AffiliateName and the ExternalLinks[Table]Name are always the same. AffiliateProgram [表] AffiliateName和ExternalLinks [表]名称始终相同。

This is how i was setting it up but i don't get it to work yet and got stuck now for the last days. 这就是我如何设置它,但我还没有让它工作,并在最后几天卡住了。

[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;
    }

this results in the following in postmaster, with 4 empty results: 这导致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"
}
]

Your query is returning an anonymous object and you are accessing attributes not being returned by the query. 您的查询返回一个匿名对象,并且您正在访问查询未返回的属性。 You could change your query to return the following attributes and then access them: 您可以更改查询以返回以下属性,然后访问它们:

        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
                    };

You have a number of syntax errors that would prevent this code from even compiling, so it's a little difficult to figure out exactly where you're going wrong, in general. 您有许多语法错误会阻止此代码进行编译,因此通常很难弄清楚您出错的确切位置。 At least make sure your code compiles first, and then if you're still not getting the results you need, we can help you. 至少要确保你的代码首先编译 ,然后如果你还没有得到你需要的结果,我们可以帮助你。

That said, it looks like you're making this much more difficult than it needs to be. 也就是说,看起来你正在变得比它需要的更困难。 If you want to be able to join the two tables, you should create a foreign key between them. 如果您希望能够加入这两个表,则应在它们之间创建一个外键。 If a particular link belongs to a particular affiliate program, then actually create that relationship: 如果特定链接属于特定联盟计划,则实际创建该关系:

public class ExternalLink
{
    ...

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

Then, you can simply include the affiliate program when you query and call it a day: 然后,您可以在查询并在一天中调用它时简单地包含联盟计划:

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

Then, as you're iterating over the links: 然后,当您迭代链接时:

var affiliateCode = link.AffiliateProgram.AffiliateCode;

Super simple. 超级简单。

Well got it to work (had to change lots of stuff actually), thank you S.Dav for the explication how to connect the other database table and modify the string with the data. 好了它工作(实际上必须更改很多东西),谢谢S.Dav的解释如何连接其他数据库表并修改字符串与数据。 Very useful lesson. 非常有用的一课。

    [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;
    }

Results in a perfect web-service with the affiliate code connected to the iTunes in this case :-) Would love to have a solution tho that could actually find the affiliate code itself but this works for now :-) 在这种情况下,结果是一个完美的网络服务,联盟代码连接到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"
 }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM