使用LINQ将数组作为参数列表搜索列表

使用LINQ将数组作为参数列表搜索列表,linq,c#-4.0,Linq,C# 4.0,我现在有一些代码看起来像这样 string[] contains = new string[]{"marge", "homer", "lisa", "bart", "maggie"}; string[] actions = new string[]{"get dye", "mmm beer", "blow saxophone", "have a cow", "bang"}; for (int i = 0; i < someActions.Count; ++i) { if (someA

我现在有一些代码看起来像这样

string[] contains = new string[]{"marge", "homer", "lisa", "bart", "maggie"};
string[] actions = new string[]{"get dye", "mmm beer", "blow saxophone", "have a cow", "bang"};

for (int i = 0; i < someActions.Count; ++i)
{
  if (someActions.Contains(contains[i]))
    callRoutine(actions[i]);
}
问题是我不知道如何使用数组作为搜索参数。各种搜索都表明我需要使用IEnumerable来实现这一点,但我不确定

任何帮助都将不胜感激


Paul

我不确定您的目的是什么,但如果您想将for循环转换为linq语句,可以执行以下操作:

var i = 0;

someActions.ForEach(x =>
                        {
                            if (someActions.Contains(contains[i]))
                               callRoutine(actions[i]);
                            i++;
                        });


这与您当前的数据设置不兼容

如果您在数据方面灵活,可以尝试以下方法:

var namesToActions = new Dictionary<string, string>()
    {
        { "marge" , "get dye" },
        { "homer", "mmm beer"},
        { "lisa", "blow saxophone"},
        { "bart", "have a cow"},
        { "maggie", "bang"}
    };

someActions.ForEach(a => callRoutine(namesToActions[a]));
var namesToActions=newdictionary()
{
{“marge”,“get dye”},
{“荷马”,“嗯,啤酒”},
{“丽莎”,“吹萨克斯管”},
{“巴特”,“生一头牛”},
{“玛吉”,“砰”}
};
someActions.ForEach(a=>callRoutine(namesToActions[a]);

切换到字典可以让您更轻松地执行正在查找的Linq操作类型,并提供额外的灵活性和更快的查找时间。

只是想知道。。。为什么要使用LINQ?不确定LINQ是否适合您在提供的特定示例中使用的堆栈。请阅读以下内容:可能提供更快的查找时间(请参阅)。
someActions.Intersect(contains).ForEach(callRoutine);
someActions.Intersect(contains).ForEach(i=>callRoutine(i));
var namesToActions = new Dictionary<string, string>()
    {
        { "marge" , "get dye" },
        { "homer", "mmm beer"},
        { "lisa", "blow saxophone"},
        { "bart", "have a cow"},
        { "maggie", "bang"}
    };

someActions.ForEach(a => callRoutine(namesToActions[a]));