views:

36

answers:

1

I am trying to find the list of objects which can be replaced.

Class Letter{
int ID;
string Name;
string AdvCode;
int isDeleted;
}

Class Replacers{
int ID;
string MainAdvCode;
string ReplacesAdvCode;
}

example data:

Replacers

0 455 400

1 955 400

2 955 455

LettersA
0 pack 455
1 addon 400

LettersB
0 big 955
1 pack 455

LettersC
0 addon 400
1 big   955
2 pack 455

LettersD
0 pack 455
1 none 019

solution: 
LetterA 1 isDeleted
LeterB 1 isDeleted
LetterC 0 and 2 isDeleted
LetterD --- 

such that if a Letter has and Advcode of 455 and another has a code of 400 the 400 gets marked for deletion. And then if another Letter has a 955 then the 455 gets marked for deletion and the 400 (which is already marked) is marked for deletion.

The problem is with my current code the 400 and 455 is marking itself for deletion?!?!?

Public class Main{
List<Letter> Letters;
List<Replacers> replaces;

   //select the ones to replace the replacements aka the little guys

   //check if the replacements replacer exists if yes mark deleted

   var filterMethodReplacements = new Func<Letter, bool>(IsAdvInReplacements);//Working

   var filterMethodReplacers = new Func<Letter, bool>(IsAdvInReplacers);//NOT WORKING????

   var resReplacements=letters.Where(filterMethodReplacements);//working

   foreach (Letter letter in resReplacements)
   {
         //select the Replacers aka the ones that contain the little guys

        var resReplacers = letters.Where(filterMethodReplacers);

        if (resReplacers != null)

             letter.isDeleted = 1;
    }

        private bool IsAdvInReplacements(Letter letter)
        {
            return (from a in Replacables where a.ReplaceAdvCode == letter.AdvCode select a).Any();
        }
        private bool IsAdvInReplacers(Letter letter)
        {
            //??????????????????????????????
            return (from a in Replacables where a.MainAdvCode == letter.AdvCode select a).Any();
        }
}
+1  A: 

See below, solution was to group by name and then iterate over the replacers.

    public class Letter
    {
        public int ID;
        public string Name;
        public string AdvCode;
        public string Type;
        public Boolean isDeleted;

        public override string ToString()
        {
            return this.Name + "[" + this.ID + "]" + ":" + this.Type + " AdvCode=" + AdvCode + " Deleted: " +  this.isDeleted.ToString();
        }
    }

    public class Replacers
    {
        public int ID;
        public string MainAdvCode;
        public string ReplacesAdvCode;
    }

class Program
{

static void Main(string[] args)
    {
        List<Letter> letters = GetLetters();
        List<Replacers> replacables = GetReplacers();



        foreach (IGrouping<string, Letter> g in letters.GroupBy(x => x.Name))
        {
            List<Letter> byName = g.ToList();


            foreach (Replacers r in replacables)
            {
                if (byName.Any(x => x.AdvCode == r.MainAdvCode) && byName.Any(x=>x.AdvCode==r.ReplacesAdvCode))
                {
                    //If we contain the code to replace...
                    foreach (Letter letter in byName.Where(x=>x.AdvCode==r.ReplacesAdvCode)){
                        letter.isDeleted = true;
                    }
                }
            }
        }

        Console.WriteLine("Result");
        foreach (Letter l in letters.Where(x=>x.isDeleted))
        {
            Console.WriteLine(l.ToString());
        }
        Console.WriteLine("Press key to close");
        Console.ReadKey();

  }

    public static List<Letter> GetLetters()
    {
        List<Letter> letters = new List<Letter>(){
            new Letter(){
                Name = "LettersA",
                ID = 0,
                AdvCode="455",
                Type="pack",
                isDeleted = false
            },
            new Letter(){
                Name = "LettersA",
                Type="addon",
                ID = 1,
                AdvCode="400",
                isDeleted = false
            },
              new Letter(){
                Name = "LettersB",
                ID = 0,
                AdvCode="955",
                Type="big",
                isDeleted = false
            },
            new Letter(){
                Name = "LettersB",
                Type="pack",
                ID = 1,
                AdvCode="455",
                isDeleted = false
            },
              new Letter(){
                Name = "LettersC",
                ID = 0,
                AdvCode="400",
                Type="addon",
                isDeleted = false
            },
            new Letter(){
                Name = "LettersC",
                Type="big",
                ID = 1,
                AdvCode="955",
                isDeleted = false
            },
             new Letter(){
                Name = "LettersC",
                Type="pack",
                ID = 2,
                AdvCode="455",
                isDeleted = false
            },
              new Letter(){
                Name = "LettersD",
                ID = 0,
                AdvCode="455",
                Type="pack",
                isDeleted = false
            },
            new Letter(){
                Name = "LettersD",
                Type="none",
                ID = 1,
                AdvCode="019",
                isDeleted = false
            },
        };

        return letters;

    }

    public static List<Replacers> GetReplacers()
    {
        return new List<Replacers>(){
            new Replacers(){
                ID = 0,
                MainAdvCode = "455",
                ReplacesAdvCode = "400"
            },
            new Replacers(){
                ID = 1,
                MainAdvCode = "955",
                ReplacesAdvCode = "400"
            },
            new Replacers(){
                ID = 2,
                MainAdvCode = "955",
                ReplacesAdvCode = "455"
            },
        };
    }
Nix
Thank you so much!!!
Philip
You can thank me with acceptance ;)
Nix