C # Using LINQ Query compares records with process array result

I wrote the following code to compare records in a DataSet (i.e.) single column. And I get the following exception:

ex: "The index was outside 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;
       }
  }
+4
source share
7 answers

Although you iterated on ds.Tables[0].Rows.Count, but you use a counter for ItemArraynot for Rows, as expected,

ds.Tables[0].Rows[0].ItemArray[k].ToString()

I suggest you reconsider your logic

+2
source

You need to view your code. you did an iteration in the Rows Count table, 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;
+1
source

Linq, DataRowCollection, select, :

lstprocess.Where(p=>ds.Tables[0].Rows.AsEnumerable.Select(row=>row["ColumnName"].ToString()).Contains(p.ProcessName))
+1

, , . , , k k < ds.Tables[0].Rows.Count, ds.Tables[0].Rows[0].ItemArray[k]. .

. LINQ , .

, , . .

, :

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;
        }
    }
}
+1

ItemArray[k] , , k, , , k rows.

, , :

//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;
0

You need to select the data from the current row on which the loop is currently being iterated. You can also get data from a specific column, so you need to specify a column name, for example ds.Tables[0].Rows[k]["columnName"].ToString()). Replace "columnName" with the 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;
0
source

Try

var pro = from p in lstprocess
          where (p.ProcessName.Contains(ds.Tables[0].Rows[k][0].ToString()))
          select p;
0
source

All Articles