ASP.NET Handling lambdas

What you need to clear the code needed for the next email ID to verify email IDs.

Mistake:   var validemails = emails.Where(p=>IsValidFormat(p)).Select;

 Dictionary<int, string> emails = new Dictionary<int, string>();
 emails.Add(1, "Marry@yahoo.com");
 emails.Add(2, "Helan@gmail.com");
 emails.Add(3, "Rose");
 emails.Add(4, "Ana");
 emails.Add(5, "Dhia@yahoo.com");




public static bool IsValidFormat(string InputEmailID)

{
    var format =
                 @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}.\.[0-9]{1,3}\.)|
                 (([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

                  Regex Rex=new Regex(format);
                  return Rex.IsMatch(InputEmailID);

 }

Error Report: Cannot convert from 'System.Collections.Generic.KeyValuePair' to string '

0
source share
4 answers

You need to send the function string, not KeyValuePair

var validemails = emails.Where(p=>IsValidFormat(p.Value)).Select(kv => kv.Value);
+2
source

Check it out here on the MSDN forums: KeyValuePair VS DictionaryEntry

0
source

, - , IEnumerable<string>

- :

var validemails = emails.Where(p=>IsValidFormat(p.Key));

var validemails = emails.Where(p=>IsValidFormat(p.Value));

, "emailid" .

:

public static bool IsValidEmail(this string InputEmailID)
{
    var format =
             @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}.\.[0-9]{1,3}\.)|
             (([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

    Regex Rex=new Regex(format);
    return Rex.IsMatch(InputEmailID);
}

, :

var validemails = emails.Where(p=>p.Key.IsValidEmail());
0

:

using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    public class Program
    {
        private static void Main(string[] args)
        {
            var emails = new Dictionary<int, string>();

            emails.Add(1, "Marry@yahoo.com");
            emails.Add(2, "Helan@gmail.com");
            emails.Add(3, "Rose");
            emails.Add(4, "Ana");
            emails.Add(5, "Dhia@yahoo.com");

            var validemails = emails.Where(p => IsValidFormat(p.Value)).ToList();
        }


        public static bool IsValidFormat(string inputEmailId)
        {
            const string format = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}.\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";

            var rex = new Regex(format);
            return rex.IsMatch(inputEmailId);
        }
    }
}

Which seems to have worked. I am not 100% sure what is happening, but the key is that you need to use p.Value.

I'm sure someone will explain the finer details - I also hope to learn a little about it.

0
source

Source: https://habr.com/ru/post/1720511/


All Articles