locked
Like Function in C# for int Field RRS feed

  • Question

  •   

       i have one  column named customer id in customer table as int data type and i want to filter the  customer records based on customer id as wild card search pattern (eg : 123* ). I can able to search if it's string data type but i want to achieve the filter functionality with int column  using linq  to entities operation.




    Sudhesh. G
    http://gurucoders.blogspot.com


    • Edited by Sudhesh.G Monday, August 12, 2013 6:23 AM
    Friday, August 9, 2013 10:04 AM

Answers

  • Hello Sudhesh.G,

    Welcome toMSDN Support Forum.

    From your description, I notice the issue you are experiencing is that you want to know research records based on customer id as wild card search pattern.

    If I have misunderstand you, please let me know.

    As far as I know that methods like .Contrains(), .StartWith() and .EndWith() those can implement the function of the Like are just supporting for string data type.

    And it is not allowed to change the int data type to the string data type because of the rules on generating lambda tree.

    So we may have to fetch out all the data into the internal memory and then do a filter operation and it is better than using .AsEnumerable() which will visit database calling to .First () or .Single () method of the each time.

    I made a sample and please see the sample code below:

    var users = (from u in op.User
                                 join c in op.CostCentre on u.Username.Trim() equals c.Userid.Trim()
                                 select new { u.Username, CostCentres = c, password = u.Password }).ToList().Where(user => user.password.ToString().Contains("11111"));

    My table data:

    Result:

    The password is an int data type in my database and both records have the “11111”.

    Or if you do not want to fetch out all the data into the internal memory then you can use SqlFunctions.StringConvert() like:

    var users = (from u in op.User
                                 join c in op.CostCentre on u.Username.Trim() equals c.Userid.Trim()
                                 where SqlFunctions.StringConvert((double)(u.Password)).Contains("11111")
                                 select new { u.Username, CostCentres = c, Password = u.Password });

    The result is the same.

    More information regarding SqlFunctions.StringConvert():

    http://msdn.microsoft.com/en-us/library/dd487158.aspx

    You can replace password with your customer id in your program.

    If this does not work, could you please share your table structure, app codes so that we can help you better.

    I look forward to hearing from you.

    Best Regards.


    Fred Bao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, August 14, 2013 1:29 AM

All replies

  • Not exactly sure what you are asking please restate the question.

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, August 9, 2013 6:30 PM
  • Hello Sudhesh.G,

    Welcome toMSDN Support Forum.

    From your description, I notice the issue you are experiencing is that you want to know research records based on customer id as wild card search pattern.

    If I have misunderstand you, please let me know.

    As far as I know that methods like .Contrains(), .StartWith() and .EndWith() those can implement the function of the Like are just supporting for string data type.

    And it is not allowed to change the int data type to the string data type because of the rules on generating lambda tree.

    So we may have to fetch out all the data into the internal memory and then do a filter operation and it is better than using .AsEnumerable() which will visit database calling to .First () or .Single () method of the each time.

    I made a sample and please see the sample code below:

    var users = (from u in op.User
                                 join c in op.CostCentre on u.Username.Trim() equals c.Userid.Trim()
                                 select new { u.Username, CostCentres = c, password = u.Password }).ToList().Where(user => user.password.ToString().Contains("11111"));

    My table data:

    Result:

    The password is an int data type in my database and both records have the “11111”.

    Or if you do not want to fetch out all the data into the internal memory then you can use SqlFunctions.StringConvert() like:

    var users = (from u in op.User
                                 join c in op.CostCentre on u.Username.Trim() equals c.Userid.Trim()
                                 where SqlFunctions.StringConvert((double)(u.Password)).Contains("11111")
                                 select new { u.Username, CostCentres = c, Password = u.Password });

    The result is the same.

    More information regarding SqlFunctions.StringConvert():

    http://msdn.microsoft.com/en-us/library/dd487158.aspx

    You can replace password with your customer id in your program.

    If this does not work, could you please share your table structure, app codes so that we can help you better.

    I look forward to hearing from you.

    Best Regards.


    Fred Bao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Wednesday, August 14, 2013 1:29 AM