Link to home
Start Free TrialLog in
Avatar of Allan
AllanFlag for United States of America

asked on

C# LINQ - Get One Record The Meets Condition, If Not Just Select One Record

Hi Experts!

Need your help with the following query.

In jobHistories, for each City, for each Perid, how would you filter the linq query to return the following condition: if Hired = "Y" then select one of the record with Hired = "Y" and ignore the rest of the records for PerId. If there are no Hired= "Y" then just select one record for the PerId
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject2
{

    [TestClass]
    public class UnitTest3
    {
        internal class JobHistory
        {
            public int PerId;
            public string JobTitle;
            public string Address;
            public string Hired;
            public string City;
            public Nullable<System.DateTime> CreatedOn;
        }

        [TestMethod]
        public void Extract_HiredThenEverythingElse_CorrectPicks2()

        {
            // Arrange
            List<JobHistory> jobHistories = new List<JobHistory>()
            {
                new JobHistory() {PerId = 1, JobTitle = "Knight", Address = "123 Street", Hired = "Y", City = "Yokohama", CreatedOn = new DateTime(2011, 6, 1)},
                new JobHistory() {PerId = 1, JobTitle = "Cook", Address = "234 Street", Hired = "Y", City = "Yokohama", CreatedOn = new DateTime(2012, 6, 1)},
                new JobHistory() {PerId = 1, JobTitle = "Mechanic", Address = "345 Street", Hired = "N", City = "Yokohama", CreatedOn = new DateTime(2012, 12, 1)},

                new JobHistory() {PerId = 2, JobTitle = "Knight", Address = "123 Street", Hired = "N", City = "Fukuoka", CreatedOn = new DateTime(2011, 6, 1)},
                new JobHistory() {PerId = 2, JobTitle = "Cook", Address = "234 Street", Hired = "N",  City = "Fukuoka", CreatedOn = new DateTime(2012, 6, 1)},
                new JobHistory() {PerId = 2, JobTitle = "Mechanic", Address = "345 Street", Hired = "",  City = "Fukuoka", CreatedOn = new DateTime(2012, 12, 1)},

                new JobHistory() {PerId = 3, JobTitle = "Knight", Address = "123 Street", Hired = "",  City = "Osaka", CreatedOn = new DateTime(2011, 6, 1)},
                new JobHistory() {PerId = 3, JobTitle = "Cook", Address = "234 Street", Hired = "", City = "Osaka", CreatedOn = new DateTime(2012, 6, 1)},
                new JobHistory() {PerId = 3, JobTitle = "Mechanic", Address = "34r Street", Hired = "", City = "Osaka", CreatedOn = new DateTime(2014, 12, 1)}
            };

            // Act
            //For each City, for each PerId, if Hired = "Y" then select one of the record with Hired = "Y". If there are no Hired= "Y" then just select one record
            var groupedByCity = (from city in jobHistories
                                   group city by city.City into islandGroup
                                   select islandGroup).ToList();

            var groupedByPerson = (from byCity in groupedByCity
                                     select from item in byCity
                                            group item by item.PerId into personGroup
                                            select personGroup.First()).ToList(); //<-- ????

            var flattened = from grouping in groupedByPerson
                            from item in grouping
                            select item;

            var results = flattened.ToList();

            //Assert
            //these are the records should return from the linq query
            //new JobHistory() { PerId = 1, JobTitle = "Knight", Address = "123 Street", Hired = "Y", City = "Yokohama", CreatedOn = new DateTime(2011, 6, 1) },
            //new JobHistory() { PerId = 2, JobTitle = "Knight", Address = "123 Street", Hired = "N", City = "Fukuoka", CreatedOn = new DateTime(2011, 6, 1) },
            //new JobHistory() { PerId = 3, JobTitle = "Knight", Address = "123 Street", Hired = "", City = "Osaka", CreatedOn = new DateTime(2011, 6, 1) },

        }

    }
}

Open in new window

Avatar of Rikin Shah
Rikin Shah
Flag of India image

Hi,

Have you tried following?

                      var groupedByPerson = (from byCity in groupedByCity
                                     select from item in byCity
                                            group item by item.PerId into personGroup
                                            select personGroup).FirstOrDefault();

Open in new window


refer: https://www.dotnetperls.com/firstordefault
Avatar of Allan

ASKER

Rikin Shah, that will not do. if needs to check of all the jobs for the person if there's Hired='y', and if there is then take that record. However, if there isn't, then just take any record. Thanks!
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Allan

ASKER

Thank you; we'll try it out now ...
Avatar of Allan

ASKER

Thank you very much it_saige!
Avatar of Allan

ASKER

Thank you very much!