locked
Linq query based on a slightly more complex data structure RRS feed

  • Question

  • User369345748 posted

    I have a list of employees with the following structure : 


     public class Person {
                   public string first_name { get; set; }
                   public string last_name { get; set; }
                   public string employeeID { get; set; }
                   public Position[] positions { get; set; }
    }

    public class Position {
                public string position_id { get; set; }
                public string job_title { get; set; }
                public string team { get; set; }
                public Person manager{ get; set; }
    }


    How do I build a linq query to retrieve the first_name , last_name and  job_title  of any employee who is managed by the manager whose employee id  is "123"

    Any help greatly appreciated 

    Thanks Richard

    Wednesday, July 11, 2018 11:22 AM

Answers

  • User-369506445 posted

    hi

    you can do a join in your query below like :

     var q = (from person in list 
                         let manager = person.positions.Where(x => x.manager.employeeID == "123").ToList() 
                         where manager.Any() 
                         select person).ToList();

    <g class="gr_ gr_68 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="68" data-gr-id="68">also</g> I create a template for <g class="gr_ gr_95 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="95" data-gr-id="95">test</g> that

    Position position1=new Position()
                {
                    job_title = "job_title1",position_id ="position_id1",team = "team1",
                    manager = new Person()
                    {
                        first_name = "first_name1" , last_name = "last_name1" , employeeID ="123"
                    }
                };
                Position position2=new Position()
                {
                    job_title = "job_title2",position_id ="position_id2",team = "team2",
                    manager = new Person()
                    {
                        first_name = "first_name2" , last_name = "last_name2" , employeeID ="employeeID2"
                    }
                };
                
                 List<Person> list=new List<Person>()
                 {
                     new Person(){first_name = "first_name1" , last_name = "last_name1" , employeeID ="employeeID1" ,  positions = new Position[]{position1}},
                          new Person(){first_name = "first_name2" , last_name = "last_name2" , employeeID ="employeeID2" ,  positions = new Position[]{position2}}
                 };
    
    
    
                var q = (from person in list 
                         let manager = person.positions.Where(x => x.manager.employeeID == "123").ToList() 
                         where manager.Any() 
                         select person).ToList();

    foreach (var person in q)
    {
    var first_name = person.first_name;
    var last_name = person.last_name;
    var employeeID = person.employeeID;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2018 4:52 PM

All replies

  • User-369506445 posted

    hi

    you can do a join in your query below like :

     var q = (from person in list 
                         let manager = person.positions.Where(x => x.manager.employeeID == "123").ToList() 
                         where manager.Any() 
                         select person).ToList();

    <g class="gr_ gr_68 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="68" data-gr-id="68">also</g> I create a template for <g class="gr_ gr_95 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="95" data-gr-id="95">test</g> that

    Position position1=new Position()
                {
                    job_title = "job_title1",position_id ="position_id1",team = "team1",
                    manager = new Person()
                    {
                        first_name = "first_name1" , last_name = "last_name1" , employeeID ="123"
                    }
                };
                Position position2=new Position()
                {
                    job_title = "job_title2",position_id ="position_id2",team = "team2",
                    manager = new Person()
                    {
                        first_name = "first_name2" , last_name = "last_name2" , employeeID ="employeeID2"
                    }
                };
                
                 List<Person> list=new List<Person>()
                 {
                     new Person(){first_name = "first_name1" , last_name = "last_name1" , employeeID ="employeeID1" ,  positions = new Position[]{position1}},
                          new Person(){first_name = "first_name2" , last_name = "last_name2" , employeeID ="employeeID2" ,  positions = new Position[]{position2}}
                 };
    
    
    
                var q = (from person in list 
                         let manager = person.positions.Where(x => x.manager.employeeID == "123").ToList() 
                         where manager.Any() 
                         select person).ToList();

    foreach (var person in q)
    {
    var first_name = person.first_name;
    var last_name = person.last_name;
    var employeeID = person.employeeID;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 11, 2018 4:52 PM
  • User-330142929 posted

    Hi Richard,

    From your description, I don't know how you sign a person to be managed by someone else, which makes it impossible for me to write a query for this kind of application scenario. Could you please post more description and codes about your needs? It is best to give some sample examples so that I can understand your needs very well.

    Look forward to your reply.

    Best Regards

    Abraham.

    Thursday, July 12, 2018 10:48 AM
  • User369345748 posted

    Thank you for you're reply .  This is helpful

    Thursday, July 12, 2018 10:51 AM
  • User369345748 posted

    Thanks Abraham

    The Person class contains a field called positions - which is array of class Position.  The Position class contains a field of Class Person, called manager.  

    So my query was about finding the positions managed by the manager "123" , & then listing the names of the people with those positions

    But I think I have enough to be working on. 

    Thanks again

    Thursday, July 12, 2018 10:57 AM
  • User-330142929 posted

    Hi Richard,

    Thanks for reply. As Vahid says, it is a great idea, and provides an effective solution.

    How do I build a linq query to retrieve the first_name , last_name and  job_title  of any employee who is managed by the manager whose employee id  is "123"

    If you want to show job_title.

    I think these code snippets may help you.

    List<Position> positions = new List<Position>() { position1, position2 };
    var result1 = list.Where(x => x.positions.Any(p => p.manager.employeeID == "123")).Join(positions, p => "123", x => x.manager.employeeID, (p, x) => new { p.first_name, p.last_name, x.job_title }).Select(o => o).ToList();

    Best Regards

    Abraham. 

    Thursday, July 12, 2018 11:58 AM