C# lambda - small question

Markus Freitag 3,786 Reputation points
2022-04-27T05:47:45.79+00:00

Hello,

Need I here ToString() override?
Why can't I debug this? I can't get into GetAge.

//~~~~~

      public void Test()
            {

                User[] users =
                    {
                  new User (1, "John", "London", "2001-04-01"),
                  new User(2, "Lenny", "New York", "1997-12-11"),
                  new User(3, "Andrew", "Boston", "1987-02-22"),
                  new User(4, "Peter", "Prague", "1936-03-24"),
                  new User(5, "Anna", "Bratislava", "1973-11-18"),
                  new User(6, "Albert", "Bratislava", "1940-12-11"),
                  new User(7, "Adam", "Trnava", "1983-12-01"),
                  new User(8, "Robert", "Bratislava", "1935-05-15"),
                  new User(9, "Robert", "Prague", "1998-03-14")
                };

                var country = "Bratislava";
                Func<User, bool> livesIn = e => e.Country == country;

                var res = users.Where(livesIn);

                foreach (var e in res)
                {
                    Console.WriteLine(e);
                }


                var users2 = new List<User>
                        {
                          new User(1, "John", "London", "2001-04-01"),
                          new User(2, "Lenny", "New York", "1997-12-11"),
                          new User(3, "Andrew", "Boston", "1987-02-22"),
                          new User(4, "Peter", "Prague", "1936-03-24"),
                          new User (5, "Anna", "Bratislava", "1973-11-18"),
                          new User (6, "Albert", "Bratislava", "1940-12-11"),
                          new User(7, "Adam", "Trnava", "1983-12-01"),
                          new User (8, "Robert", "Bratislava", "1935-05-15"),
                          new User(9, "Robert", "Prague", "1998-03-14"),
                        };

                var age2 = 60;
                Func<User, bool> olderThan = e => GetAge(e) > age2;

                var res2 = users.Where(olderThan);

                foreach (var e in res)
                {
                    Console.WriteLine(e);
// Need I here ToString() override?
                }

            }

//Why can't I debug this? I can't get into GetAge.

      int GetAge(User user)
            {
                var dob = DateTime.Parse(user.Birthday);
                return (int)Math.Floor((DateTime.Now - dob).TotalDays / 365.25D);
            }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,835 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,036 Reputation points
    2022-04-28T14:10:54.297+00:00

    Here is a simple example done in a console project.

    The console project uses two class project, BaseDataValidatorLibrary for validating models and uses System.ComponentModel.Annotations NuGet package, The second class project, BaseModelsLibrary has various models to validate.

    If this works for you, clone the repository, all you need is the BaseDataValidatorLibrary class project.

    Here is the example from my first reply in a console project.
    197396-screenshot.png

    using System;  
    using BaseDataValidatorLibrary.Helpers;  
    using BaseModelsLibrary.Models;  
    using static System.DateTime;  
      
    namespace PersonValidationConsole  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                PersonIsValid();  
                Console.WriteLine();  
                PersonIsNotValid();  
                  
                Console.ReadLine();  
            }  
      
            private static void PersonIsNotValid()  
            {  
                Title(nameof(PersonIsNotValid));  
                Person person = Person;  
                person.BirthDate = new DateTime(1931, Now.Month, Now.Day);  
      
                EntityValidationResult result = Model.Validate(person);  
      
                if (result.IsValid)  
                {  
                    Console.WriteLine("Valid");  
                }  
                else  
                {  
                    foreach (var error in result.Errors)  
                    {  
                        Console.WriteLine(error.ErrorMessage);  
                    }  
                }  
            }  
      
            private static void PersonIsValid()  
            {  
                Title(nameof(PersonIsValid));  
                Person person = Person;  
                  
                EntityValidationResult result = Model.Validate(person);  
      
                if (result.IsValid)  
                {  
                    Console.WriteLine("Valid");  
                }  
                else  
                {  
                    foreach (var error in result.Errors)  
                    {  
                        Console.WriteLine(error.ErrorMessage);  
                    }  
                }  
            }  
      
            private static Person Person => new()  
            {  
                FirstName = "Mike",  
                LastName = "Flowers",  
                BirthDate = new DateTime(1932, Now.Month, Now.Day)  
            };  
      
            private static void Title(string sender)  
            {  
                Console.ForegroundColor = ConsoleColor.White;  
                Console.Write("Running");  
                Console.ResetColor();  
                Console.Write($" {sender}\n");  
            }  
        }  
    }  
      
    

    Note that there are a good deal of code samples in the repository to learn from, most are done in unit test and one in ASP.NET Core Razor.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2022-04-27T11:52:28.117+00:00

    There is no built in tools to debug into your method. Best recommendation is to use LINQPad and use it's Dump method or OzCode.

    1 person found this answer helpful.