1

I found this question that is similar to what I'm asking, but not exactly the same: Is there some way I can "join" the contents of two javascript arrays much like I would do a join in SQL

In that question, the user provides two arrays that they want to join in an SQL-like manner.

My difference is that I have several APIs set up to reference a database. I need to get the APIs to "join" on a value.

Here's a rough sample of the classes:

public partial class test_review
{
    [Key]
    public int rating_id { get; set; }
    public int site_id { get; set; }
    public string comments { get; set; }
}

and:

public partial class test_site
{
    [Key]
    public int site_id { get; set; }
    [Required]
    [StringLength(3)]
    public string site_name { get; set; }
}

If I was writing this in SQL, I would do something like

SELECT r.rating_id, s.site_name, r.comments 
FROM test_review r
INNER JOIN test_site s on r.site_id = s.site_id

Currently, I have code that looks like this in my ReadReviews.aspx page:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script src="Scripts/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        function getReviews() {
            $.getJSON("api/review",
                function (data) {
                    $('#reviews').empty();
                    $.each(data, function (key, val) {
                        var row = '<td>' + val.rating_id + '</td><td>' + val.airport_id + '</td>';
                        $('<tr/>', { html: row })
                            .appendTo($('#reviews'));
                    });
                });
        }
        $(document).ready(getReviews);
    </script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <h2> Here are the API reviews</h2>
            <table>
                <thead> 
                    <tr>    
                        <th>Rating_id</th>
                        <th>airport_id</th>
                    </tr>
                </thead>
                <tbody id ="reviews">
                </tbody>
            </table>

</asp:Content>

(Note: this jquery code came from https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/using-web-api-with-aspnet-web-forms)

This is great and all, but not exactly what I'm looking for in this case. I need to combine information from the two APIs.

Would it have been better to create a few views? I know that I CAN do that, but I'm not sure that it's better/more efficient/faster than pulling directly from the tables and combining them somehow.

phroureo
  • 377
  • 3
  • 16
  • [linq.js - LINQ for JavaScript](https://github.com/mihaifm/linq) – Reza Aghaei Aug 13 '18 at 19:03
  • 1
    Merge the 2 datasets with C# and then send the data to the browser. – VDWWD Aug 13 '18 at 19:04
  • @VDWWD Where would I do that? Do I need to create a new class to do it in? Or would I have to do it in the ReadReviews.aspx.cs page somehow? – phroureo Aug 13 '18 at 20:05
  • Are these models/classes in separate solutions? If not then cpuld you use LINQ join something https://stackoverflow.com/a/26094503/470014 ? – Caltor Aug 13 '18 at 20:49
  • Depends, but you can create a new class or inherit one from the other. There are a lot of ways to do that. – VDWWD Aug 13 '18 at 20:53

1 Answers1

0

What I ended up doing was creating a SQL view and making a second API specifically for the view to return all the data I needed.

phroureo
  • 377
  • 3
  • 16