Entity Framework Join two tables together for displaying in MVC view











up vote
0
down vote

favorite












I am new to Entity Framework and LINQ and I am having some difficulty in linking two tables together for a view so that it can be displayed to the end user. I have a list of prices which are displayed in Euro in one table and in the other table I have an exchange rate table. Currently this is only displayed in Euro and I would like to display this in GBP.



Previously I was able to return the single view table from my controller using the following:



return View(db.Prices.ToList());


Now that I am attempting to return the two tables I am not sure what I need to do but I have tried the following code to join the tables:



var result = (from a in db.Prices
join b in db.ExchangeRatse on a.TRADE_DATE equals b.TRADE_DATE


The Prices class does not have a column for GBP price so I need to multiply the euro price by the exchange rate for the trading day and display this in my view but the backing model of the view is set to Prices, would I be better to come up with a view model for this or could it be done simpler?










share|improve this question


























    up vote
    0
    down vote

    favorite












    I am new to Entity Framework and LINQ and I am having some difficulty in linking two tables together for a view so that it can be displayed to the end user. I have a list of prices which are displayed in Euro in one table and in the other table I have an exchange rate table. Currently this is only displayed in Euro and I would like to display this in GBP.



    Previously I was able to return the single view table from my controller using the following:



    return View(db.Prices.ToList());


    Now that I am attempting to return the two tables I am not sure what I need to do but I have tried the following code to join the tables:



    var result = (from a in db.Prices
    join b in db.ExchangeRatse on a.TRADE_DATE equals b.TRADE_DATE


    The Prices class does not have a column for GBP price so I need to multiply the euro price by the exchange rate for the trading day and display this in my view but the backing model of the view is set to Prices, would I be better to come up with a view model for this or could it be done simpler?










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am new to Entity Framework and LINQ and I am having some difficulty in linking two tables together for a view so that it can be displayed to the end user. I have a list of prices which are displayed in Euro in one table and in the other table I have an exchange rate table. Currently this is only displayed in Euro and I would like to display this in GBP.



      Previously I was able to return the single view table from my controller using the following:



      return View(db.Prices.ToList());


      Now that I am attempting to return the two tables I am not sure what I need to do but I have tried the following code to join the tables:



      var result = (from a in db.Prices
      join b in db.ExchangeRatse on a.TRADE_DATE equals b.TRADE_DATE


      The Prices class does not have a column for GBP price so I need to multiply the euro price by the exchange rate for the trading day and display this in my view but the backing model of the view is set to Prices, would I be better to come up with a view model for this or could it be done simpler?










      share|improve this question













      I am new to Entity Framework and LINQ and I am having some difficulty in linking two tables together for a view so that it can be displayed to the end user. I have a list of prices which are displayed in Euro in one table and in the other table I have an exchange rate table. Currently this is only displayed in Euro and I would like to display this in GBP.



      Previously I was able to return the single view table from my controller using the following:



      return View(db.Prices.ToList());


      Now that I am attempting to return the two tables I am not sure what I need to do but I have tried the following code to join the tables:



      var result = (from a in db.Prices
      join b in db.ExchangeRatse on a.TRADE_DATE equals b.TRADE_DATE


      The Prices class does not have a column for GBP price so I need to multiply the euro price by the exchange rate for the trading day and display this in my view but the backing model of the view is set to Prices, would I be better to come up with a view model for this or could it be done simpler?







      c# entity-framework model-view-controller






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 10:41









      Seán-Pól

      31




      31
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You need something like:



          var result = (from a in db.Prices
          join b in db.ExchangeRatse on a.TRADE_DATE equals b.TRADE_DATE)
          .Select(x => new {
          PriceInEuro = x.a.PriceInEuro,
          PriceInSterling = x.a.PriveInEuro * x.b.ExchangeRate})
          .ToList();


          Warning - not tested.






          share|improve this answer





















          • cheers just created a view model and did something similar to this
            – Seán-Pól
            Nov 8 at 12:16











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206033%2fentity-framework-join-two-tables-together-for-displaying-in-mvc-view%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          You need something like:



          var result = (from a in db.Prices
          join b in db.ExchangeRatse on a.TRADE_DATE equals b.TRADE_DATE)
          .Select(x => new {
          PriceInEuro = x.a.PriceInEuro,
          PriceInSterling = x.a.PriveInEuro * x.b.ExchangeRate})
          .ToList();


          Warning - not tested.






          share|improve this answer





















          • cheers just created a view model and did something similar to this
            – Seán-Pól
            Nov 8 at 12:16















          up vote
          0
          down vote



          accepted










          You need something like:



          var result = (from a in db.Prices
          join b in db.ExchangeRatse on a.TRADE_DATE equals b.TRADE_DATE)
          .Select(x => new {
          PriceInEuro = x.a.PriceInEuro,
          PriceInSterling = x.a.PriveInEuro * x.b.ExchangeRate})
          .ToList();


          Warning - not tested.






          share|improve this answer





















          • cheers just created a view model and did something similar to this
            – Seán-Pól
            Nov 8 at 12:16













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          You need something like:



          var result = (from a in db.Prices
          join b in db.ExchangeRatse on a.TRADE_DATE equals b.TRADE_DATE)
          .Select(x => new {
          PriceInEuro = x.a.PriceInEuro,
          PriceInSterling = x.a.PriveInEuro * x.b.ExchangeRate})
          .ToList();


          Warning - not tested.






          share|improve this answer












          You need something like:



          var result = (from a in db.Prices
          join b in db.ExchangeRatse on a.TRADE_DATE equals b.TRADE_DATE)
          .Select(x => new {
          PriceInEuro = x.a.PriceInEuro,
          PriceInSterling = x.a.PriveInEuro * x.b.ExchangeRate})
          .ToList();


          Warning - not tested.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 11:00









          Peter Smith

          3,43453963




          3,43453963












          • cheers just created a view model and did something similar to this
            – Seán-Pól
            Nov 8 at 12:16


















          • cheers just created a view model and did something similar to this
            – Seán-Pól
            Nov 8 at 12:16
















          cheers just created a view model and did something similar to this
          – Seán-Pól
          Nov 8 at 12:16




          cheers just created a view model and did something similar to this
          – Seán-Pól
          Nov 8 at 12:16


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206033%2fentity-framework-join-two-tables-together-for-displaying-in-mvc-view%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff