简体   繁体   中英

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

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

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.

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()) . Replace "columnName" with actual column name.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM