linq委托从对象检查函数

时间:2010-05-14 17:16:52

标签: linq function delegates

我正在尝试找到可以替换的对象列表。

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

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

示例数据:

代用品

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 --- 

如果一封信有,且Advcode为455而另一封信的代码为400,则400被标记为删除。然后,如果另一封信有一个955,则455被标记为删除,而400(已经标记)被标记为删除。

问题在于我目前的代码400和455是否标记为删除?!?!?

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 个答案:

答案 0 :(得分:1)

见下文,解决方案是按名称分组,然后迭代替换器。

    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"
            },
        };
    }