[英]C# Using LINQ Query compare the records with result of process array
I have written following code to compare the records of DataSet (ie) record of one column. 我编写了以下代码来比较一列DataSet(即)记录的记录。 And I am getting following Exception:
我得到以下例外:
ex:" Index was outside the bounds of the array."
例如:“索引超出了数组的范围。”
public void GetRunningTask()
{
// Process[] lstprocess = Process.GetProcesses();
conn=new SqlConnection("Data Source=.; Initial Catalog='TTES'; Integrated Security=SSPI;");
da=new SqlDataAdapter("Select AppName from LRNSetting", conn);
ds=new DataSet();
da.Fill(ds,"LRNSetting");
// Process[] lstprocess = Process.GetProcesses();
for (int k = 0; k < ds.Tables[0].Rows.Count; k++)
{
Process[] lstprocess = Process.GetProcesses();
// DataRow dr=ds.Tables[0].Rows.Cast<DataRow>().Single(row=>row["AppName"])
var pro = from p in lstprocess
//where p.ProcessName.Contains("LRCDual")
//where p.ProcessName.Contains(ds.Tables[0].Rows[k].ItemArray) //added temporary
where (p.ProcessName.Contains(ds.Tables[0].Rows[0].ItemArray[k].ToString()))
select p;
}
}
Although you made the iteration on ds.Tables[0].Rows.Count
but you are using the counter for ItemArray
not for Rows
as expected, 虽然您在
ds.Tables[0].Rows.Count
上进行了迭代,但是您正在使用ItemArray
的计数器而不是按预期方式使用Rows
ds.Tables[0].Rows[0].ItemArray[k].ToString()
I suggest you to review your logic 我建议你检查一下你的逻辑
You need to review your code. 您需要检查您的代码。 you made the iteration on table's Rows Count but you are using the counter for ItemArray not for Rows as expected, Replace
你在表的Rows Count上进行了迭代,但是你正在使用ItemArray的计数器而不是像预期的那样使用Rows
var pro = from p in lstprocess
//where p.ProcessName.Contains("LRCDual")
//where p.ProcessName.Contains(ds.Tables[0].Rows[k].ItemArray) //added temporary
where (p.ProcessName.Contains(ds.Tables[0].Rows[0].ItemArray[k].ToString()))
select p;
this code with 这段代码用
var pro = from p in lstprocess
//where p.ProcessName.Contains("LRCDual")
//where p.ProcessName.Contains(ds.Tables[0].Rows[k].ItemArray) //added temporary
where (p.ProcessName.Contains(ds.Tables[0].Rows[k].ItemArray['CollumnName'].ToString()))
select p;
简单的Linq查询,使DataRowCollection可枚举,应用select以获取具有进程名称的给定列的列表并与原始进程名称进行比较:
lstprocess.Where(p=>ds.Tables[0].Rows.AsEnumerable.Select(row=>row["ColumnName"].ToString()).Contains(p.ProcessName))
You seem to have a few issues with your code. 您的代码似乎有些问题。 First up, as others have said, you are using index
k
with the bound k < ds.Tables[0].Rows.Count
but you are using it against ds.Tables[0].Rows[0].ItemArray[k]
. 首先,正如其他人所说的那样,你正在使用带有绑定
k < ds.Tables[0].Rows.Count
索引k
,但你正在使用它来对抗ds.Tables[0].Rows[0].ItemArray[k]
。 They are two different things. 他们是两个不同的东西。
You are better off not using indexes like this. 最好不要使用这样的索引。 You are using LINQ for part of your code, but you could use it for the rest.
您正在使用LINQ作为部分代码,但您可以将其用于其余部分。
Also you seem to not want to dispose of any of your disposable objects. 此外,您似乎不想丢弃任何一次性物品。 You must ensure all disposables are disposed of.
您必须确保丢弃所有一次性用品。
So, try this: 所以,试试这个:
using (var conn = new SqlConnection("Data Source=.; Initial Catalog='TTES'; Integrated Security=SSPI;"))
{
using (var da = new SqlDataAdapter("Select AppName from LRNSetting", conn))
{
using (var ds = new DataSet())
{
da.Fill(ds,"LRNSetting");
var appNames =
ds
.Tables[0]
.Rows
.Cast<DataRow>()
.Select(x => x[0].ToString())
.ToArray();
var pro =
from p in Process.GetProcesses()
where appNames.Any(x => p.ProcessName.Contains(x))
select p;
}
}
}
Using ItemArray[k]
means you assumed you have k
columns but as your code shows, you have k
rows. 使用
ItemArray[k]
意味着您假设您有k
列但是如您的代码所示,您有k
行。
So This must be what you are looking for: 所以这一定是你要找的东西:
//Getting all table cells for every column and row as string
var tableValues = ds.Tables[0].AsEnumerable()
.SelectMany(i => i.ItemArray.Select(j => j.ToString()))
.ToList();
Process[] lstprocess = Process.GetProcesses();
var pro = from p in lstprocess
where tableValues.Any(i => p.ProcessName.Contains(i))
select p;
You need to select data from current row on which loop is currently iterating. 您需要从当前正在迭代循环的当前行中选择数据。 Also you may want to get data from a specific column so you need to specify column name like
ds.Tables[0].Rows[k]["columnName"].ToString())
. 此外,您可能希望从特定列获取数据,因此您需要指定列名,如
ds.Tables[0].Rows[k]["columnName"].ToString())
。 Replace "columnName" with actual column name. 将“columnName”替换为实际列名。
var pro = from p in lstprocess
//where p.ProcessName.Contains("LRCDual")
//where p.ProcessName.Contains(ds.Tables[0].Rows[k].ItemArray) //added temporary
where (p.ProcessName.Contains(ds.Tables[0].Rows[k]["columnName"].ToString()))
select p;
Try 尝试
var pro = from p in lstprocess
where (p.ProcessName.Contains(ds.Tables[0].Rows[k][0].ToString()))
select p;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.