none
How to compare string in LINQ query? RRS feed

  • Question

  • Is this LINQ query correct?

    session = sessionlist.Find(x => x.Phone == from);

    session is element of List<session>

    session.Phone and from is string.

    Wednesday, June 5, 2019 3:24 AM

Answers

  • If the phone# doesn't exist in the list, the result is null, is it right?

    Here is a way to assert against null

    var sessionList = new List<Session>
    {
        new Session {phone = "123"},
        new Session {phone = "123"},
        new Session {phone = "1934"},
        new Session {phone = "7569"}
    };
    var findValue = "123";
    var result = sessionList.Find(item => item.phone.Equals(findValue));
    Console.WriteLine(result?.phone ?? "Not found");
    And even better to setup in a unit test

    [TestMethod]
    public void FindItemInSessionListTestMethod()
    {
        var sessionList = new List<Session>
        {
            new Session {phone = "123"},
            new Session {phone = "123"},
            new Session {phone = "1234"},
            new Session {phone = "45678"}
        };
        var findValue = "123";
        var result = sessionList.Find(item => item.phone.Equals(findValue));
        Assert.IsNotNull(result);
    } 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jeff0803 Friday, June 7, 2019 3:31 AM
    Wednesday, June 5, 2019 6:31 PM
    Moderator
  • Hi Jeff0803,

    Thank you for posting here.

    Based on your description, you want to compare string in LINQ query.

    You could try the following code.

    public class Session
            {
                public  string phone { get; set; }
    
            }
    
            static void Main(string[] args)
            {
                List<Session> sessionlist = new List<Session>();
                sessionlist.Add(new Session {phone="123" });
                sessionlist.Add(new Session { phone = "123" });
                sessionlist.Add(new Session { phone = "1234" });
                sessionlist.Add(new Session { phone = "456789" });
                string from = "123";
                var result = sessionlist.Find(s => s.phone.Equals(from));
                Console.WriteLine(result.phone);
                Console.ReadKey();
            }

    Output:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, June 5, 2019 5:58 AM
    Moderator

All replies

  • Hi Jeff0803,

    Thank you for posting here.

    Based on your description, you want to compare string in LINQ query.

    You could try the following code.

    public class Session
            {
                public  string phone { get; set; }
    
            }
    
            static void Main(string[] args)
            {
                List<Session> sessionlist = new List<Session>();
                sessionlist.Add(new Session {phone="123" });
                sessionlist.Add(new Session { phone = "123" });
                sessionlist.Add(new Session { phone = "1234" });
                sessionlist.Add(new Session { phone = "456789" });
                string from = "123";
                var result = sessionlist.Find(s => s.phone.Equals(from));
                Console.WriteLine(result.phone);
                Console.ReadKey();
            }

    Output:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, June 5, 2019 5:58 AM
    Moderator
  • If the phone# doesn't exist in the list, the result is null, is it right?
    • Proposed as answer by Deepedro Monday, June 10, 2019 12:59 PM
    Wednesday, June 5, 2019 3:52 PM
  • If the phone# doesn't exist in the list, the result is null, is it right?

    Here is a way to assert against null

    var sessionList = new List<Session>
    {
        new Session {phone = "123"},
        new Session {phone = "123"},
        new Session {phone = "1934"},
        new Session {phone = "7569"}
    };
    var findValue = "123";
    var result = sessionList.Find(item => item.phone.Equals(findValue));
    Console.WriteLine(result?.phone ?? "Not found");
    And even better to setup in a unit test

    [TestMethod]
    public void FindItemInSessionListTestMethod()
    {
        var sessionList = new List<Session>
        {
            new Session {phone = "123"},
            new Session {phone = "123"},
            new Session {phone = "1234"},
            new Session {phone = "45678"}
        };
        var findValue = "123";
        var result = sessionList.Find(item => item.phone.Equals(findValue));
        Assert.IsNotNull(result);
    } 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Jeff0803 Friday, June 7, 2019 3:31 AM
    Wednesday, June 5, 2019 6:31 PM
    Moderator