locked
Trim all the fields in LINQ Select query RRS feed

  • Question

  • Hello All,

    I need some help on the linq sql queries. I just started using LINQ in my project and wrote the below query which returns 182 fields from the table;

    var entityType = (from C in ctx.myTableName

                  select C).FirstOrDefault();

    I want all the field values should be trimmed. Is it possible to do it in the query above or do I need to trim the individual fields when I use them.

    Wednesday, December 4, 2013 6:53 AM

Answers

  • Hello,

    >>Is it possible to do it in the query above or do I need to trim the individual fields when I use them.

    Yes, it is possible. We can do it like below:

    var entityType = (from C in ctx.myTableName
    
                  select new {Porperty1= C.Property1.Trim(),Porperty2= C.Property2.Trim(),...}).FirstOrDefault();

    However, you have said that the table has 182 fileds, so it should be a huge project and waste our time.

    So, in my opinion, I suggest trimming the individual fields when we use them.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by S Prashant Wednesday, December 4, 2013 9:32 AM
    Wednesday, December 4, 2013 8:56 AM
  • Hi,

    You should use both Reflection and Extension methods.

    Here you have an example:

    Quickly Trim all your model's string properties

    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.


    • Marked as answer by S Prashant Wednesday, December 4, 2013 9:32 AM
    • Edited by JA Reyes Wednesday, December 4, 2013 9:37 AM syntax
    Wednesday, December 4, 2013 9:08 AM

All replies

  • Hello,

    >>Is it possible to do it in the query above or do I need to trim the individual fields when I use them.

    Yes, it is possible. We can do it like below:

    var entityType = (from C in ctx.myTableName
    
                  select new {Porperty1= C.Property1.Trim(),Porperty2= C.Property2.Trim(),...}).FirstOrDefault();

    However, you have said that the table has 182 fileds, so it should be a huge project and waste our time.

    So, in my opinion, I suggest trimming the individual fields when we use them.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by S Prashant Wednesday, December 4, 2013 9:32 AM
    Wednesday, December 4, 2013 8:56 AM
  • Hi,

    You should use both Reflection and Extension methods.

    Here you have an example:

    Quickly Trim all your model's string properties

    Regards.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.


    • Marked as answer by S Prashant Wednesday, December 4, 2013 9:32 AM
    • Edited by JA Reyes Wednesday, December 4, 2013 9:37 AM syntax
    Wednesday, December 4, 2013 9:08 AM
  • Thanks for the help,

    I will trim the field values when they are used.

    Wednesday, December 4, 2013 9:32 AM