How to identify one item from a list based on some criteria in C#

One07k-4914 101 Reputation points
2023-03-17T15:03:37.82+00:00

Hi ,

According to some criteria, I have to find out one item from a list.

For more information, please see my inline comments.

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace BranchFinder

{

    class Program

    {

        static void Main(string[] args)

        {

            var file = new List<FileChanges>

            {

                new FileChanges

                {

                    LocalFilePath =

                        @"D:\LD\WF\ARM\Modules\Reference\MM\DD\Client\Events\Publisher.cs",

                    ServerFilePath =

                        "$/TD/Dev/ARM/ARM/Modules/Reference/MM/DD/Client/Events/Publisher.cs"

                },

                new FileChanges

                {

                    LocalFilePath =

                        @"D:\LD\WF\ARM\Modules\Reference\ModuleCommon.sln",

                    ServerFilePath = "$/TD/Dev/ARM/ARM/Modules/Reference/ModuleCommon.sln"

                }

            };

            var branches = new List<Branches>

            {

                new Branches

                {

                    Id=1,

                    LocalItem = @"D:\LD\WF",

                    ServerItem = "$/TD/Dev/WF"

                },

                new Branches

                {

                    Id=2,

                    LocalItem = @"D:\LD\WF\ARM",

                    ServerItem = "$/TD/Dev/ARM/ARM"

                }

            };

            var b = GetBranch(file, branches);

            Console.ReadLine();

        }

        private static object GetBranch(List<FileChanges> file, List<Branches> branches)

        {



            //TODO:My requirement is to retrive from branches where Id=2;


            /*  Details
            Filtering criteria:

For Eg:

Id = 2

Branches.LocalItem = @"D:\LD\WF\ARM"

FileChange.LocalFilePath = @"D:\LD\WF\ARM\Modules\Reference\MM\DD\Client\Events\Publisher.cs"

Both Branches.LocalItem and FileChange.LocalFilePath should starts exactly same.

//Below approach is not working becoz

Id = 1

Branches.LocalItem = @"D:\LD\WF",

FileChange.LocalFilePath = @"D:\LD\WF\ARM\Modules\Reference\MM\DD\Client\Events\Publisher.cs"

            var s = branches.Where(branch =>

                   file.Any(x => x.LocalFilePath.Contains(branch.LocalItem)))

               .Distinct().ToList();

            Both LocalItem @"D:\LD\WF" and  @"D:\LD\WF\ARM" are starts with same string  @"D:\LD\WF".

Any solution will be helpful.

    */




        }

    }

    public class FileChanges

    {

        public string LocalFilePath { get; set; }

        public string ServerFilePath { get; set; }

    }

    public class Branches

    {

        public int Id { get; set; }

        public string LocalItem { get; set; }

        public string ServerItem { get; set; }

    }

}

//includedChanges

//LocalFilePath -D:\LD\WF\ARM\Modules\Reference\MM\DD\Client\Events\Publisher.cs

//ServerFilePath- $/TD/Dev/ARM/ARM/Modules/Reference/MM/DD/Client/Events/Publisher.cs

//includedChanges

//LocalFilePath -D:\LD\WF\ARM\Modules\Reference\ModuleCommon.sln

//ServerFilePath- $/TD/Dev/ARM/ARM/Modules/Reference/ModuleCommon.sln

//Branch

//LocalItem -D:\LD\WF

//ServerItem- $/TD/Dev/WF

//Branch

//LocalItem -D:\LD\WF\ARM

//ServerItem- $/TD/Dev/ARM/ARM

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2023-03-20T06:37:44.71+00:00

    Hi @One07k-4914 ,

    The following code filters Branches that match all of the corresponding properties of List<includeChanges>.

    If I misunderstand your question, please point it out.

            private static object GetBranch(List<FileChanges> file, List<Branches> branches) {
    
                return branches.Where(x => x.test1(file)).ToList();
    
            }
    
        public class Branches {
    
            public int Id { get; set; }
    
            public string LocalItem { get; set; }
    
            public string ServerItem { get; set; }
    
            public bool test1(List<FileChanges> fc) {
                bool res = true;
                foreach (FileChanges fileChanges in fc) {
                    if (!fileChanges.LocalFilePath.Contains(LocalItem) || !fileChanges.ServerFilePath.Contains(ServerItem)) {
                        res = false;
                        break;
                    }
                }
    
                return res;
            }
    
        }
    

    Best Regards.
    Jiachen Li
    ----------
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.