how to convert lambda expression with statement body to an expression tree












0















I have problem with my code, I don't know problem because am new to c#, But am trying to convert a project from Asp.Net MVC to Core. can someone help me with this code.



        using (var context = new UniversityDbContext())
{
var payments =
(from p in context.Payments
.FromSql(
"select * from dbo.Payments p inner join dbo.Bills b on p.BillId = b.Id where p.OrderStatus = 'Approved' order by p.Service")
join b in context.Bills on p.BillId equals b.Id
where b.User == userId
select new { p, b })

//error is from this .Select(x =>
{
x.p.Bill = x.b;
return x.p;
}).ToList(); // To this

var t = payments.Count();


return payments;
}









share|improve this question

























  • Share us your current model defination. For Asp.Net, you could define many-to-many relationship directly. But for asp.net core, you need to define middle class to implement many-to-many relationship. You need to redesign your model.

    – Tao Zhou
    Dec 31 '18 at 2:32
















0















I have problem with my code, I don't know problem because am new to c#, But am trying to convert a project from Asp.Net MVC to Core. can someone help me with this code.



        using (var context = new UniversityDbContext())
{
var payments =
(from p in context.Payments
.FromSql(
"select * from dbo.Payments p inner join dbo.Bills b on p.BillId = b.Id where p.OrderStatus = 'Approved' order by p.Service")
join b in context.Bills on p.BillId equals b.Id
where b.User == userId
select new { p, b })

//error is from this .Select(x =>
{
x.p.Bill = x.b;
return x.p;
}).ToList(); // To this

var t = payments.Count();


return payments;
}









share|improve this question

























  • Share us your current model defination. For Asp.Net, you could define many-to-many relationship directly. But for asp.net core, you need to define middle class to implement many-to-many relationship. You need to redesign your model.

    – Tao Zhou
    Dec 31 '18 at 2:32














0












0








0








I have problem with my code, I don't know problem because am new to c#, But am trying to convert a project from Asp.Net MVC to Core. can someone help me with this code.



        using (var context = new UniversityDbContext())
{
var payments =
(from p in context.Payments
.FromSql(
"select * from dbo.Payments p inner join dbo.Bills b on p.BillId = b.Id where p.OrderStatus = 'Approved' order by p.Service")
join b in context.Bills on p.BillId equals b.Id
where b.User == userId
select new { p, b })

//error is from this .Select(x =>
{
x.p.Bill = x.b;
return x.p;
}).ToList(); // To this

var t = payments.Count();


return payments;
}









share|improve this question
















I have problem with my code, I don't know problem because am new to c#, But am trying to convert a project from Asp.Net MVC to Core. can someone help me with this code.



        using (var context = new UniversityDbContext())
{
var payments =
(from p in context.Payments
.FromSql(
"select * from dbo.Payments p inner join dbo.Bills b on p.BillId = b.Id where p.OrderStatus = 'Approved' order by p.Service")
join b in context.Bills on p.BillId equals b.Id
where b.User == userId
select new { p, b })

//error is from this .Select(x =>
{
x.p.Bill = x.b;
return x.p;
}).ToList(); // To this

var t = payments.Count();


return payments;
}






asp.net-mvc asp.net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 11:24









Neel Darji

63111




63111










asked Dec 28 '18 at 10:51









Ayomide MatanmiAyomide Matanmi

11




11













  • Share us your current model defination. For Asp.Net, you could define many-to-many relationship directly. But for asp.net core, you need to define middle class to implement many-to-many relationship. You need to redesign your model.

    – Tao Zhou
    Dec 31 '18 at 2:32



















  • Share us your current model defination. For Asp.Net, you could define many-to-many relationship directly. But for asp.net core, you need to define middle class to implement many-to-many relationship. You need to redesign your model.

    – Tao Zhou
    Dec 31 '18 at 2:32

















Share us your current model defination. For Asp.Net, you could define many-to-many relationship directly. But for asp.net core, you need to define middle class to implement many-to-many relationship. You need to redesign your model.

– Tao Zhou
Dec 31 '18 at 2:32





Share us your current model defination. For Asp.Net, you could define many-to-many relationship directly. But for asp.net core, you need to define middle class to implement many-to-many relationship. You need to redesign your model.

– Tao Zhou
Dec 31 '18 at 2:32












2 Answers
2






active

oldest

votes


















0














if Context.Payments.Bills is defined
then you can use the code below



  context.Payments.FromSql(
"select * from dbo.Payments p where p.OrderStatus = 'Approved' order by p.Service").Include(x => x.Bills);


needs to return Bills as well inside Payments p.Bills



Reference: https://docs.microsoft.com/en-us/ef/core/querying/raw-sql#including-related-data






share|improve this answer































    0














    For lambda expression with statement body to an expression tree this issue, you could not pass x.p.Bill = x.b; to linq query statement, try something like below:



    using (var context = new UniversityDbContext())
    {
    var payments =
    (from p in context.Payments
    .FromSql(
    "select * from dbo.Payments p inner join dbo.Bills b on p.BillId = b.Id where p.OrderStatus = 'Approved' order by p.Service")
    join b in context.Bills on p.BillId equals b.Id
    where b.User == userId
    select new { p, b })
    .ToList()
    .Select(x =>
    {
    x.p.Bill = x.b;
    return x.p;
    }).ToList(); // To this

    var t = payments.Count();
    return payments;
    }





    share|improve this answer























      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',
      autoActivateHeartbeat: false,
      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%2f53957349%2fhow-to-convert-lambda-expression-with-statement-body-to-an-expression-tree%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      if Context.Payments.Bills is defined
      then you can use the code below



        context.Payments.FromSql(
      "select * from dbo.Payments p where p.OrderStatus = 'Approved' order by p.Service").Include(x => x.Bills);


      needs to return Bills as well inside Payments p.Bills



      Reference: https://docs.microsoft.com/en-us/ef/core/querying/raw-sql#including-related-data






      share|improve this answer




























        0














        if Context.Payments.Bills is defined
        then you can use the code below



          context.Payments.FromSql(
        "select * from dbo.Payments p where p.OrderStatus = 'Approved' order by p.Service").Include(x => x.Bills);


        needs to return Bills as well inside Payments p.Bills



        Reference: https://docs.microsoft.com/en-us/ef/core/querying/raw-sql#including-related-data






        share|improve this answer


























          0












          0








          0







          if Context.Payments.Bills is defined
          then you can use the code below



            context.Payments.FromSql(
          "select * from dbo.Payments p where p.OrderStatus = 'Approved' order by p.Service").Include(x => x.Bills);


          needs to return Bills as well inside Payments p.Bills



          Reference: https://docs.microsoft.com/en-us/ef/core/querying/raw-sql#including-related-data






          share|improve this answer













          if Context.Payments.Bills is defined
          then you can use the code below



            context.Payments.FromSql(
          "select * from dbo.Payments p where p.OrderStatus = 'Approved' order by p.Service").Include(x => x.Bills);


          needs to return Bills as well inside Payments p.Bills



          Reference: https://docs.microsoft.com/en-us/ef/core/querying/raw-sql#including-related-data







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 28 '18 at 11:39









          SimonareSimonare

          11.3k11737




          11.3k11737

























              0














              For lambda expression with statement body to an expression tree this issue, you could not pass x.p.Bill = x.b; to linq query statement, try something like below:



              using (var context = new UniversityDbContext())
              {
              var payments =
              (from p in context.Payments
              .FromSql(
              "select * from dbo.Payments p inner join dbo.Bills b on p.BillId = b.Id where p.OrderStatus = 'Approved' order by p.Service")
              join b in context.Bills on p.BillId equals b.Id
              where b.User == userId
              select new { p, b })
              .ToList()
              .Select(x =>
              {
              x.p.Bill = x.b;
              return x.p;
              }).ToList(); // To this

              var t = payments.Count();
              return payments;
              }





              share|improve this answer




























                0














                For lambda expression with statement body to an expression tree this issue, you could not pass x.p.Bill = x.b; to linq query statement, try something like below:



                using (var context = new UniversityDbContext())
                {
                var payments =
                (from p in context.Payments
                .FromSql(
                "select * from dbo.Payments p inner join dbo.Bills b on p.BillId = b.Id where p.OrderStatus = 'Approved' order by p.Service")
                join b in context.Bills on p.BillId equals b.Id
                where b.User == userId
                select new { p, b })
                .ToList()
                .Select(x =>
                {
                x.p.Bill = x.b;
                return x.p;
                }).ToList(); // To this

                var t = payments.Count();
                return payments;
                }





                share|improve this answer


























                  0












                  0








                  0







                  For lambda expression with statement body to an expression tree this issue, you could not pass x.p.Bill = x.b; to linq query statement, try something like below:



                  using (var context = new UniversityDbContext())
                  {
                  var payments =
                  (from p in context.Payments
                  .FromSql(
                  "select * from dbo.Payments p inner join dbo.Bills b on p.BillId = b.Id where p.OrderStatus = 'Approved' order by p.Service")
                  join b in context.Bills on p.BillId equals b.Id
                  where b.User == userId
                  select new { p, b })
                  .ToList()
                  .Select(x =>
                  {
                  x.p.Bill = x.b;
                  return x.p;
                  }).ToList(); // To this

                  var t = payments.Count();
                  return payments;
                  }





                  share|improve this answer













                  For lambda expression with statement body to an expression tree this issue, you could not pass x.p.Bill = x.b; to linq query statement, try something like below:



                  using (var context = new UniversityDbContext())
                  {
                  var payments =
                  (from p in context.Payments
                  .FromSql(
                  "select * from dbo.Payments p inner join dbo.Bills b on p.BillId = b.Id where p.OrderStatus = 'Approved' order by p.Service")
                  join b in context.Bills on p.BillId equals b.Id
                  where b.User == userId
                  select new { p, b })
                  .ToList()
                  .Select(x =>
                  {
                  x.p.Bill = x.b;
                  return x.p;
                  }).ToList(); // To this

                  var t = payments.Count();
                  return payments;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 31 '18 at 3:09









                  Tao ZhouTao Zhou

                  5,91131230




                  5,91131230






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53957349%2fhow-to-convert-lambda-expression-with-statement-body-to-an-expression-tree%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas