【发布时间】:2016-05-31 20:02:19
【问题描述】:
对于我为工作所做的一个项目,我必须制作一份报告,从 SSIS 脚本组件中获取主管列表。不幸的是,这一功能给我带来了问题,我不知道为什么。下面的函数从 SSIS 输入缓冲区中获取一行,并检查一个名为 supervisors 的列表变量,以查看该实例中的名称是否已经在名为 supervisors 的列表中,如果没有,则将输入缓冲区行中的名称放入列表。出于某种原因,它只是添加了所有通过输入缓冲区的人,我真的不知道为什么!
例如,我必须有一份员工名单及其主管姓名:
- 弗雷德·弗林特斯通 |饼干怪兽
- 约翰·多伊 |苦力怪物
- 威利旺卡|橡皮鸭
我想要得到的结果类似于 group by..cookie monster, rubber duckie
相反,我得到了这个......饼干怪物,饼干怪物,橡皮鸭
public void check(Input0Buffer Row)
{
bool flag = true;
string expression = String.Format("({0}|{1})", Row.FirstName, Row.LastName);
Regex lookup = new Regex(expression,RegexOptions.IgnoreCase);
if (supervisors.Count > 0)
{
foreach (string person in supervisors)
{
if (lookup.Matches(person).Count >= 2)
{
flag = false;
break;
}
}
if (flag)
supervisors.Add(Row.ReportsToName);
}
else
supervisors.Add(Row.ReportsToName);
}
这是来自 SSIS 脚本的所有代码。
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
/// <summary>
/// This is the class to which to add your code. Do not change the name, attributes, or parent
/// of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
public List<Dictionary<string, string>> Employees;
public List<string> supervisors;
string temp;
/// <summary>
/// This method is called once, before rows begin to be processed in the data flow.
///
/// You can remove this method if you don't need to do anything here.
/// </summary>
public override void PreExecute()
{
base.PreExecute();
Employees = new List<Dictionary<string, string>>();
supervisors = new List<string>();
temp = "First Name\tLast Name\tEXT\tUsername\n";
}
/// <summary>
/// This method is called after all the rows have passed through this component.
///
/// You can delete this method if you don't need to do anything here.
/// </summary>
public override void PostExecute()
{
base.PostExecute();
foreach (string supervisor in supervisors)
{
foreach (Dictionary<string, string> employee in Employees)
{
if(match(supervisor,employee))
{
temp+= String.Format("{0}\t{1}\t{2}\t{3}\n",employee["First"],employee["last"],employee["EXT"],employee["Username"]);
break;
}
}
}
File.WriteAllLines(@"blahvlajsdlfh", (string[])temp.Split('\n'));
File.AppendAllLines(@"Q:gfdhdfghdfgh", supervisors.ToArray());
}
/// <summary>
/// This method is called once for every row that passes through the component from Input0.
///
/// Example of reading a value from a column in the the row:
/// string zipCode = Row.ZipCode
///
/// Example of writing a value to a column in the row:
/// Row.ZipCode = zipCode
/// </summary>
/// <param name="Row">The row that is currently passing through the component</param>
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
Dictionary<string, string> temp_dict = new Dictionary<string, string>();
temp_dict.Add("First", Row.FirstName);
temp_dict.Add("last", Row.LastName);
if (Row.ext_IsNull)
temp_dict.Add("EXT", "0000");
else
temp_dict.Add("EXT", Row.ext);
if (Row.OEEUSERNAME_IsNull)
temp_dict.Add("Username", "not available");
else
temp_dict.Add("Username", Row.OEEUSERNAME);
Employees.Add(temp_dict);
check(Row);
}
/// <summary>
/// checks to see if a person is the supervisor list.
/// </summary>
/// <param name="Row"></param>
public void check(Input0Buffer Row)
{
bool flag = true;
string expression = String.Format("({0}|{1})", Row.FirstName, Row.LastName);
Regex lookup = new Regex(expression,RegexOptions.IgnoreCase);
if (supervisors.Count > 0)
{
foreach (string person in supervisors)
{
if (lookup.Matches(person).Count >= 2)
{
flag = false;
break;
}
}
if (flag)
supervisors.Add(Row.ReportsToName);
}
else
supervisors.Add(Row.ReportsToName);
}
/// <summary>
/// used to match supervisors to all of there information
/// </summary>
/// <param name="supervisor"></param>
/// <param name="employees"></param>
/// <returns></returns>
public bool match(string supervisor, Dictionary<string, string> employees)
{
string expression = String.Format("({0}|{1})", employees["First"], employees["last"]);
Regex lookup = new Regex(expression, RegexOptions.IgnoreCase);
if (lookup.Matches(supervisor).Count >= 2)
return true;
return false;
}
}
【问题讨论】:
-
尝试
string expression = String.Format("({0}.*{1}|{1}.*{0})", Row.FirstName, Row.LastName);然后if (lookup.IsMatch(person))... -
是的,没用。没有一个人出现
-
如果您可以缩小问题范围,并提供一些示例输入和示例输出,那么帮助您会更容易。
-
看起来和我给出的例子差不多。 “|”只是在那里表明它是第一个值的结束。我真的不认为正则表达式是现在的问题。程序对 string.contains() 方法做同样的事情。