How to dynamically create an Expression<Func>










0















I have a class like this:



public class Student

public int Id get; set;
public string Name get; set;
public string LOANIDBPM get; set;
public string REPORTTYPE get; set;



I have a list Students,
I want to filter some student in list by LOANIDBPM and REPORTTYPE.



Usually, this is the code (using linq)



public void GetListStudent(List<Student> listStudent)

listStudent = listStudent.Where(x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX").ToList();



However, for some reason, i can't Explicit "List",
My solution is below:



public void GetListStudent(object listStudent)

ParameterExpression param = Expression.Parameter(listStudent.First().GetType(), nameof(listStudent));
Expression propLoanBPM = Expression.Property(param, "LOANIDBPM");
Expression<Func<string>> loanIdLampda = () => "MIKEN";
Expression searchLoanBPM = Expression.Equal(propLoanBPM, loanIdLampda.Body);

Expression propReportType = Expression.Property(param, "REPORTTYPE");
Expression<Func<string>> reportTypeLampda = () => "DX";

Expression searchReportType = Expression.Equal(propReportType, reportTypeLampda.Body);

Expression searchFinal = Expression.And(searchLoanBPM, searchReportType);


Expression<Func<???, bool>> lampda = Expression.Lambda<Func<???, bool>>(searchFinal, param);

listStudent = listStudent.Where(lampda).ToArray();



The code above has two problems:



1: I don't know type of Student in Expression Func,
I can't use like:



Expression<Func< listStudent.First().GetType(), bool>> lampda = Expression.Lambda<Func< listStudent.First().GetType(), bool>>(searchFinal, param);


2: In Where method, it's need to IQueryable, but in my code is Expression Func.



Sorry by my English.
Thank you so much










share|improve this question




























    0















    I have a class like this:



    public class Student

    public int Id get; set;
    public string Name get; set;
    public string LOANIDBPM get; set;
    public string REPORTTYPE get; set;



    I have a list Students,
    I want to filter some student in list by LOANIDBPM and REPORTTYPE.



    Usually, this is the code (using linq)



    public void GetListStudent(List<Student> listStudent)

    listStudent = listStudent.Where(x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX").ToList();



    However, for some reason, i can't Explicit "List",
    My solution is below:



    public void GetListStudent(object listStudent)

    ParameterExpression param = Expression.Parameter(listStudent.First().GetType(), nameof(listStudent));
    Expression propLoanBPM = Expression.Property(param, "LOANIDBPM");
    Expression<Func<string>> loanIdLampda = () => "MIKEN";
    Expression searchLoanBPM = Expression.Equal(propLoanBPM, loanIdLampda.Body);

    Expression propReportType = Expression.Property(param, "REPORTTYPE");
    Expression<Func<string>> reportTypeLampda = () => "DX";

    Expression searchReportType = Expression.Equal(propReportType, reportTypeLampda.Body);

    Expression searchFinal = Expression.And(searchLoanBPM, searchReportType);


    Expression<Func<???, bool>> lampda = Expression.Lambda<Func<???, bool>>(searchFinal, param);

    listStudent = listStudent.Where(lampda).ToArray();



    The code above has two problems:



    1: I don't know type of Student in Expression Func,
    I can't use like:



    Expression<Func< listStudent.First().GetType(), bool>> lampda = Expression.Lambda<Func< listStudent.First().GetType(), bool>>(searchFinal, param);


    2: In Where method, it's need to IQueryable, but in my code is Expression Func.



    Sorry by my English.
    Thank you so much










    share|improve this question


























      0












      0








      0








      I have a class like this:



      public class Student

      public int Id get; set;
      public string Name get; set;
      public string LOANIDBPM get; set;
      public string REPORTTYPE get; set;



      I have a list Students,
      I want to filter some student in list by LOANIDBPM and REPORTTYPE.



      Usually, this is the code (using linq)



      public void GetListStudent(List<Student> listStudent)

      listStudent = listStudent.Where(x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX").ToList();



      However, for some reason, i can't Explicit "List",
      My solution is below:



      public void GetListStudent(object listStudent)

      ParameterExpression param = Expression.Parameter(listStudent.First().GetType(), nameof(listStudent));
      Expression propLoanBPM = Expression.Property(param, "LOANIDBPM");
      Expression<Func<string>> loanIdLampda = () => "MIKEN";
      Expression searchLoanBPM = Expression.Equal(propLoanBPM, loanIdLampda.Body);

      Expression propReportType = Expression.Property(param, "REPORTTYPE");
      Expression<Func<string>> reportTypeLampda = () => "DX";

      Expression searchReportType = Expression.Equal(propReportType, reportTypeLampda.Body);

      Expression searchFinal = Expression.And(searchLoanBPM, searchReportType);


      Expression<Func<???, bool>> lampda = Expression.Lambda<Func<???, bool>>(searchFinal, param);

      listStudent = listStudent.Where(lampda).ToArray();



      The code above has two problems:



      1: I don't know type of Student in Expression Func,
      I can't use like:



      Expression<Func< listStudent.First().GetType(), bool>> lampda = Expression.Lambda<Func< listStudent.First().GetType(), bool>>(searchFinal, param);


      2: In Where method, it's need to IQueryable, but in my code is Expression Func.



      Sorry by my English.
      Thank you so much










      share|improve this question
















      I have a class like this:



      public class Student

      public int Id get; set;
      public string Name get; set;
      public string LOANIDBPM get; set;
      public string REPORTTYPE get; set;



      I have a list Students,
      I want to filter some student in list by LOANIDBPM and REPORTTYPE.



      Usually, this is the code (using linq)



      public void GetListStudent(List<Student> listStudent)

      listStudent = listStudent.Where(x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX").ToList();



      However, for some reason, i can't Explicit "List",
      My solution is below:



      public void GetListStudent(object listStudent)

      ParameterExpression param = Expression.Parameter(listStudent.First().GetType(), nameof(listStudent));
      Expression propLoanBPM = Expression.Property(param, "LOANIDBPM");
      Expression<Func<string>> loanIdLampda = () => "MIKEN";
      Expression searchLoanBPM = Expression.Equal(propLoanBPM, loanIdLampda.Body);

      Expression propReportType = Expression.Property(param, "REPORTTYPE");
      Expression<Func<string>> reportTypeLampda = () => "DX";

      Expression searchReportType = Expression.Equal(propReportType, reportTypeLampda.Body);

      Expression searchFinal = Expression.And(searchLoanBPM, searchReportType);


      Expression<Func<???, bool>> lampda = Expression.Lambda<Func<???, bool>>(searchFinal, param);

      listStudent = listStudent.Where(lampda).ToArray();



      The code above has two problems:



      1: I don't know type of Student in Expression Func,
      I can't use like:



      Expression<Func< listStudent.First().GetType(), bool>> lampda = Expression.Lambda<Func< listStudent.First().GetType(), bool>>(searchFinal, param);


      2: In Where method, it's need to IQueryable, but in my code is Expression Func.



      Sorry by my English.
      Thank you so much







      c# linq reflection func






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 3:26







      mikenlanggio

















      asked Nov 14 '18 at 2:57









      mikenlanggiomikenlanggio

      12




      12






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Your first example doesn't compile because listStudent is a list and LINQ is generally going to return an IEnumerable of some kind. You need to add ToList():



          public void GetListStudent(List<Student> listStudent)

          listStudent = listStudent
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToList(); //<-- add this



          Your second example doesn't work because c# doesn't know that your object array is actually a list of students. You can tell it using the Cast<>() method. When you're done, you need to convert back to an array, so add ToArray() at the end.



          public void GetListStudent(object listStudent)

          listStudent = listStudent
          .Cast<Student>() //<-- add this
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToArray(); //<-- and this



          If only some of the objects are known to be students, you can use OfType<>() to filter them out.



          public void GetListStudent(object listStudent)

          listStudent = listStudent
          .OfType<Student>() //<-- add this
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToArray(); //<-- and this



          And if you have absolutely no clue what those objects are but are quite certain they have LOANIDBPM and REPORTTYPE properties, I suppose you could use dynamic:



          public void GetListStudent(List<dynamic> listStudent)

          listStudent = listStudent
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToList();






          share|improve this answer

























          • No. I don't know the list is Student

            – mikenlanggio
            Nov 14 '18 at 3:17











          • @mikenlanggio, May I ask how come you do not know? You obviously know that your object is of Student objects because you are filtering on LOANIDBPM and REPORTTYPE

            – Hasan Emrah Süngü
            Nov 14 '18 at 3:18







          • 1





            If you don't know that the objects are students, how can you possibly know to filter on LOANIDBPM or REPORTTYPE?

            – John Wu
            Nov 14 '18 at 3:19











          • I only know it sure have two property is LOANIDBPM and REPORTTYPE.

            – mikenlanggio
            Nov 14 '18 at 3:23











          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%2f53292546%2fhow-to-dynamically-create-an-expressionfunct-object%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Your first example doesn't compile because listStudent is a list and LINQ is generally going to return an IEnumerable of some kind. You need to add ToList():



          public void GetListStudent(List<Student> listStudent)

          listStudent = listStudent
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToList(); //<-- add this



          Your second example doesn't work because c# doesn't know that your object array is actually a list of students. You can tell it using the Cast<>() method. When you're done, you need to convert back to an array, so add ToArray() at the end.



          public void GetListStudent(object listStudent)

          listStudent = listStudent
          .Cast<Student>() //<-- add this
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToArray(); //<-- and this



          If only some of the objects are known to be students, you can use OfType<>() to filter them out.



          public void GetListStudent(object listStudent)

          listStudent = listStudent
          .OfType<Student>() //<-- add this
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToArray(); //<-- and this



          And if you have absolutely no clue what those objects are but are quite certain they have LOANIDBPM and REPORTTYPE properties, I suppose you could use dynamic:



          public void GetListStudent(List<dynamic> listStudent)

          listStudent = listStudent
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToList();






          share|improve this answer

























          • No. I don't know the list is Student

            – mikenlanggio
            Nov 14 '18 at 3:17











          • @mikenlanggio, May I ask how come you do not know? You obviously know that your object is of Student objects because you are filtering on LOANIDBPM and REPORTTYPE

            – Hasan Emrah Süngü
            Nov 14 '18 at 3:18







          • 1





            If you don't know that the objects are students, how can you possibly know to filter on LOANIDBPM or REPORTTYPE?

            – John Wu
            Nov 14 '18 at 3:19











          • I only know it sure have two property is LOANIDBPM and REPORTTYPE.

            – mikenlanggio
            Nov 14 '18 at 3:23
















          0














          Your first example doesn't compile because listStudent is a list and LINQ is generally going to return an IEnumerable of some kind. You need to add ToList():



          public void GetListStudent(List<Student> listStudent)

          listStudent = listStudent
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToList(); //<-- add this



          Your second example doesn't work because c# doesn't know that your object array is actually a list of students. You can tell it using the Cast<>() method. When you're done, you need to convert back to an array, so add ToArray() at the end.



          public void GetListStudent(object listStudent)

          listStudent = listStudent
          .Cast<Student>() //<-- add this
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToArray(); //<-- and this



          If only some of the objects are known to be students, you can use OfType<>() to filter them out.



          public void GetListStudent(object listStudent)

          listStudent = listStudent
          .OfType<Student>() //<-- add this
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToArray(); //<-- and this



          And if you have absolutely no clue what those objects are but are quite certain they have LOANIDBPM and REPORTTYPE properties, I suppose you could use dynamic:



          public void GetListStudent(List<dynamic> listStudent)

          listStudent = listStudent
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToList();






          share|improve this answer

























          • No. I don't know the list is Student

            – mikenlanggio
            Nov 14 '18 at 3:17











          • @mikenlanggio, May I ask how come you do not know? You obviously know that your object is of Student objects because you are filtering on LOANIDBPM and REPORTTYPE

            – Hasan Emrah Süngü
            Nov 14 '18 at 3:18







          • 1





            If you don't know that the objects are students, how can you possibly know to filter on LOANIDBPM or REPORTTYPE?

            – John Wu
            Nov 14 '18 at 3:19











          • I only know it sure have two property is LOANIDBPM and REPORTTYPE.

            – mikenlanggio
            Nov 14 '18 at 3:23














          0












          0








          0







          Your first example doesn't compile because listStudent is a list and LINQ is generally going to return an IEnumerable of some kind. You need to add ToList():



          public void GetListStudent(List<Student> listStudent)

          listStudent = listStudent
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToList(); //<-- add this



          Your second example doesn't work because c# doesn't know that your object array is actually a list of students. You can tell it using the Cast<>() method. When you're done, you need to convert back to an array, so add ToArray() at the end.



          public void GetListStudent(object listStudent)

          listStudent = listStudent
          .Cast<Student>() //<-- add this
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToArray(); //<-- and this



          If only some of the objects are known to be students, you can use OfType<>() to filter them out.



          public void GetListStudent(object listStudent)

          listStudent = listStudent
          .OfType<Student>() //<-- add this
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToArray(); //<-- and this



          And if you have absolutely no clue what those objects are but are quite certain they have LOANIDBPM and REPORTTYPE properties, I suppose you could use dynamic:



          public void GetListStudent(List<dynamic> listStudent)

          listStudent = listStudent
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToList();






          share|improve this answer















          Your first example doesn't compile because listStudent is a list and LINQ is generally going to return an IEnumerable of some kind. You need to add ToList():



          public void GetListStudent(List<Student> listStudent)

          listStudent = listStudent
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToList(); //<-- add this



          Your second example doesn't work because c# doesn't know that your object array is actually a list of students. You can tell it using the Cast<>() method. When you're done, you need to convert back to an array, so add ToArray() at the end.



          public void GetListStudent(object listStudent)

          listStudent = listStudent
          .Cast<Student>() //<-- add this
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToArray(); //<-- and this



          If only some of the objects are known to be students, you can use OfType<>() to filter them out.



          public void GetListStudent(object listStudent)

          listStudent = listStudent
          .OfType<Student>() //<-- add this
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToArray(); //<-- and this



          And if you have absolutely no clue what those objects are but are quite certain they have LOANIDBPM and REPORTTYPE properties, I suppose you could use dynamic:



          public void GetListStudent(List<dynamic> listStudent)

          listStudent = listStudent
          .Where
          (
          x => x.LOANIDBPM == "MIKEN" && x.REPORTTYPE == "DX"
          )
          .ToList();







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 3:23

























          answered Nov 14 '18 at 3:06









          John WuJohn Wu

          30.1k42752




          30.1k42752












          • No. I don't know the list is Student

            – mikenlanggio
            Nov 14 '18 at 3:17











          • @mikenlanggio, May I ask how come you do not know? You obviously know that your object is of Student objects because you are filtering on LOANIDBPM and REPORTTYPE

            – Hasan Emrah Süngü
            Nov 14 '18 at 3:18







          • 1





            If you don't know that the objects are students, how can you possibly know to filter on LOANIDBPM or REPORTTYPE?

            – John Wu
            Nov 14 '18 at 3:19











          • I only know it sure have two property is LOANIDBPM and REPORTTYPE.

            – mikenlanggio
            Nov 14 '18 at 3:23


















          • No. I don't know the list is Student

            – mikenlanggio
            Nov 14 '18 at 3:17











          • @mikenlanggio, May I ask how come you do not know? You obviously know that your object is of Student objects because you are filtering on LOANIDBPM and REPORTTYPE

            – Hasan Emrah Süngü
            Nov 14 '18 at 3:18







          • 1





            If you don't know that the objects are students, how can you possibly know to filter on LOANIDBPM or REPORTTYPE?

            – John Wu
            Nov 14 '18 at 3:19











          • I only know it sure have two property is LOANIDBPM and REPORTTYPE.

            – mikenlanggio
            Nov 14 '18 at 3:23

















          No. I don't know the list is Student

          – mikenlanggio
          Nov 14 '18 at 3:17





          No. I don't know the list is Student

          – mikenlanggio
          Nov 14 '18 at 3:17













          @mikenlanggio, May I ask how come you do not know? You obviously know that your object is of Student objects because you are filtering on LOANIDBPM and REPORTTYPE

          – Hasan Emrah Süngü
          Nov 14 '18 at 3:18






          @mikenlanggio, May I ask how come you do not know? You obviously know that your object is of Student objects because you are filtering on LOANIDBPM and REPORTTYPE

          – Hasan Emrah Süngü
          Nov 14 '18 at 3:18





          1




          1





          If you don't know that the objects are students, how can you possibly know to filter on LOANIDBPM or REPORTTYPE?

          – John Wu
          Nov 14 '18 at 3:19





          If you don't know that the objects are students, how can you possibly know to filter on LOANIDBPM or REPORTTYPE?

          – John Wu
          Nov 14 '18 at 3:19













          I only know it sure have two property is LOANIDBPM and REPORTTYPE.

          – mikenlanggio
          Nov 14 '18 at 3:23






          I only know it sure have two property is LOANIDBPM and REPORTTYPE.

          – mikenlanggio
          Nov 14 '18 at 3:23


















          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%2f53292546%2fhow-to-dynamically-create-an-expressionfunct-object%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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto