• Find all cities that starts with A small program with Linq in C#

    ShikhaTan Member

    Show each region in which there is at least one male member who lives in a city whose name starts with the letter A.

    The question to me is asking for a city that starts with A. If it’s only one table then you only need to select Region. I don’t use SQL very often, but below is how I would do it with Linq in C#.This would return both USA and Canada because there is at least one member that is a male in both USA and Canada that lives in a City that starts with A.

  • Abhey Member

    SELECT Region FROM Members WHERE Gender == ‘M’ AND City LIKE ‘A%’;

                Members Andy = new Members();
                Andy.gender = 'M';
                Andy.region = "USA";
                Andy.city = "Wherever";
                Andy.firstName = "Andy";
                Andy.lastName = "Johnson";
    
                Members Rich = new Members();
    
                Rich.gender = 'M';
                Rich.region = "USA";
                Rich.city = "Anytown";
                Rich.firstName = "Rich";
                Rich.lastName = "Smith";
    
                Members Lisa = new Members();
    
                Lisa.gender = 'F';
                Lisa.region = "Canada";
                Lisa.city = "Somewhere";
                Lisa.firstName = "Lisa";
                Lisa.lastName = "Benson";
    
                Members John = new Members();
    
                John.gender = 'M';
                John.region = "Canada";
                John.city = "Anywhere";
                John.firstName = "John";
                John.lastName = "Marx";
    
                List list = new List();
                list.Add(Andy);
                list.Add(Rich);
                list.Add(Lisa);
                list.Add(John);
    
                var results = from l in list
                where l.city.StartsWith("A") && l.gender == 'M'
                select l.region; 
    
Viewing 1 reply thread
  • You must be logged in to reply to this topic.