locked
Lambda expression / c# conversion RRS feed

  • Question

  • I have the following function PickSong, that is supposed to return some random song from my observableCollection Songs, that was not yet used. I am actually trying to convert this from c#(code below). I am unsure about this lambda expression that i wrote(converted).

    Private Function PickSong()
    
            Dim random As New Random     
            Dim unusedSongs = Songs.Where(Function(p) p.Used = False)
    
    
            Dim randomNumber = random.Next(unusedSongs.Count)
            Dim randomSong = unusedSongs.ElementAt(randomNumber)
            randomSong.Selected = True
    
            Return randomSong
    
        End Function

    C# Code

    Random random = new Random();
                var unusedSongs = Songs.Where(p => p.Used == false);
                var randomNumber = random.Next(unusedSongs.Count());
                var randomSong = unusedSongs.ElementAt(randomNumber);
                randomSong.Selected = true;
                return randomSong;
    I guess i just wanna ask is the conversion good?

    Monday, February 12, 2018 1:52 PM

Answers

  • Looks good without trying it, best to execute the code and see if it returns the expected results.

    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Tehnay Monday, February 12, 2018 2:10 PM
    Monday, February 12, 2018 1:58 PM

All replies

  • Looks good without trying it, best to execute the code and see if it returns the expected results.

    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Tehnay Monday, February 12, 2018 2:10 PM
    Monday, February 12, 2018 1:58 PM
  • I guess it is, then the problem is in the next part of my code, but I wasn't sure about that lambda, thank you for fast reply. I should probably make a new thread.
    Monday, February 12, 2018 2:16 PM
  • I guess it is, then the problem is in the next part of my code, but I wasn't sure about that lambda, thank you for fast reply. I should probably make a new thread.
    Please (as you said) make a new thread and include in that post a link back to this post.

    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 12, 2018 3:12 PM