locked
LINQ Expression RRS feed

  • Question

  • Please, i am trying to search with a unique column name from a table.

    Below is an example of an SQL statement i want to re-write using LINQ expression, but i am having difficulties.

    SELECT user_id FROM users WHERE firstname = 'Godymn'

    Will appreciate any help.

    Kind Regards,

    GNG

    Wednesday, July 18, 2018 12:31 PM

Answers

  • Hi Godymn,

    Based on your description and related code, I create a simple demo, it works well.

    I would suggest that you could debug your application and set the break point at the following line of code. then check if it return any value. and check if the connection string connect the right database.

    result = db.users.Where(x => x.firstname == link).Select(x => x.user_id).FirstOrDefault();


    Best regards,

    Zhanglong


    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.

    Friday, July 20, 2018 1:10 AM
  • Hello Ehsan,

    Thanks. Your query was correct. The reason why it initially returned a 0 was becuase the lin contains both firstname and lastname. When i tested with just firstname, it gave me the right id..., so, all i just had to do was concatenate. And this produce the right id.

    Thank you once again. I appreciate!

    Sunday, July 22, 2018 12:10 AM

All replies

  • This is so trivial. If you have an in memory collection you could do like:

    var result = users.Where(x=>x.firstname == "Godymn").Select(x=>x.userid);

    This will return you a collection back and if only single row is expected back then call FirstOrDefault() at the end.

    Hope it helps.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, July 18, 2018 12:51 PM
  • Hi,

    Many thanks for the response. I tried this but it return a 0 when i checked its value. I really don't know what went wrong. What i intend to achieve is the user id number based on the name entered into the link.

    I have made a sample code on this. See below.

     private int Chk()
            {
                
                string link = linkLabelPartner.Text;
                var result = (dynamic)null;
                using (DbModel db = new DbModel())
                {
                    result = db.users.Where(x => x.firstname == link).Select(x => x.user_id).FirstOrDefault();
                }
                return result;
                
            }

    Here is the button i used to check the content of what got returned.

    private void btnTey_Click(object sender, EventArgs e)
            {
                int test = Chk();
                MessageBox.Show(test.ToString());
            }

    Wednesday, July 18, 2018 1:51 PM
  • Your DbModel is empty so you wouldn't get any results back. Your DbModel needs to be connected to a database and then you're users property needs to be a type that queries data from the database. Is this EF or something you're trying to use?

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, July 18, 2018 2:47 PM
  • Hi,

    My DbModel is connected to a database already and yes, i use EF.

    Kind Regards,

    GNG

    Wednesday, July 18, 2018 2:57 PM
  • Is it? Is this just a wrapper around a DbContext that is already configured to connect to the DB you need? What is the type of Users? IQueryable? What does this call produce?

    new DbModel().users.ToArray();


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, July 18, 2018 3:42 PM
  • Hi,
    The == comparison operator is case sensitive. If you would like to ignore the case, then use the following query:
    result = db.users.Where(x => x.firstname.Equals(link, StringComparison.InvariantCultureIgnoreCase)).Select(x => x.user_id).FirstOrDefault();


    Wednesday, July 18, 2018 4:13 PM
  • Try This :

    private int Chk()
    {
                string link = linkLabelPartner.Text;
                var result = db.users.Where(x => x.firstname == link).Select(x => x.user_id);
                return result.Count();
    }
    private void btnTey_Click(object sender, EventArgs e)
    {
                int test = Chk();
                MessageBox.Show(test.ToString());
    }
    Wednesday, July 18, 2018 6:18 PM
  • Hi Godymn,

    Thank you for posting here.

    Since your question is more related to EF, I will move it to EF forum for suitable support.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework&filter=alltypes&sort=lastpostdesc

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Wendy


    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.

    Thursday, July 19, 2018 2:29 AM
  • Hi Godymn,

    Based on your description and related code, I create a simple demo, it works well.

    I would suggest that you could debug your application and set the break point at the following line of code. then check if it return any value. and check if the connection string connect the right database.

    result = db.users.Where(x => x.firstname == link).Select(x => x.user_id).FirstOrDefault();


    Best regards,

    Zhanglong


    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.

    Friday, July 20, 2018 1:10 AM
  • Many thanks. Yeah, this worked for me. I also had to do some concatenation since the link holds firstname and lastname.

    Yeah, thanks....I appreciate.

    Sunday, July 22, 2018 12:08 AM
  • Hello Ehsan,

    Thanks. Your query was correct. The reason why it initially returned a 0 was becuase the lin contains both firstname and lastname. When i tested with just firstname, it gave me the right id..., so, all i just had to do was concatenate. And this produce the right id.

    Thank you once again. I appreciate!

    Sunday, July 22, 2018 12:10 AM
  • Hi Godymn,

    According to your description, it seems that you resolve the issue, please mark it as answer, it will be beneficial to other communities who have the similar issue.

    Best regards,

    Zhanglong


    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.

    Monday, July 23, 2018 1:26 AM