Get All Column Names of a Datatable into String Array Using (Linq/Predicate)

Get all column names of a DataTable into string array using (LINQ/Predicate)

Try this (LINQ method syntax):

string[] columnNames = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();

or in LINQ Query syntax:

string[] columnNames = (from dc in dt.Columns.Cast<DataColumn>()
select dc.ColumnName).ToArray();

Cast is required, because Columns is of type DataColumnCollection which is a IEnumerable, not IEnumerable<DataColumn>. The other parts should be obvious.

Get DataRow if column names in a string array have matching values in a string array

This might be a good starting point, at least to better ask questions and move towards an answer.

string[] colName = { "RuleID", "GroupBy0", "GroupBy1", "GroupBy2" };

// "All the below logic is run for each row of rule"
// this goes through each row of the rule DataTable
foreach (DataRow rule in ruleTable.Rows)
{
// This is going to be equivalent to the grpby variable you specified
var groupRules = rule.Field<string>("GroupBy").ToString().Split("|");

// Some sort of mapping may need to go here to go from "ageband" to "GroupBy0", "gender" to "GroupBy1", etc.

foreach(DataRow row in dtResult.Rows)
{
DataTable distDtResult = dtResult.DefaultView.ToTable(true, colName);

var updateTEST = from dr in distDtResult.AsEnumerable()
where dr.Field<string>("RuleID") == rule["RuleID"].ToString()
&& dr.Field<string>("GroupBy0") == row["GroupBy0"].ToString() // ageband
&& dr.Field<string>("GroupBy1") == row["GroupBy1"].ToString() // gender
&& dr.Field<string>("GroupBy2") == row["GroupBy2"].ToString() // code
// more
select dr;
}
}

Get column results from DataTable into a string

Solution 1

  1. Iterate each DataRow and get the value from DataColumn.

  2. Add value into array/list.

  3. Convert array/list to string with String.Join() with ; as separator.

using System.Collections.Generic;

List<string> emails = new List<string>();

foreach (DataRow row in dt.Rows)
{
emails.Add(row["Email"].ToString());
}

string result = String.Join(";", emails);

Solution 2

  1. Working with System.Linq to get the email as List<string>.

  2. Convert array/list to string with String.Join() with ; as separator.

using System.Linq;

string result = String.Join(";", dt.AsEnumerable()
.Select(x => x["Email"].ToString())
.ToList());

Sample .NET Fiddle

Use linq to find DataTable(Name) in a DataSet using unique list of Column Names

To me sounds like you're trying to see if columnNames passed to the method are contained within Column's name collection of Table. If that's the case, this should do the work.

List<DataTable> tables =
ds.Tables
.Cast<DataTable>()
.Where(dt => !columnNames.Except(dt.Columns.Select(c => c.Name)).Any())
.ToList();

(Below is an append by the asker of the question)

Well, I had to tweak it to make it compile, but you got me there..
Thanks.

Final Answer:

        List<DataTable> tables =
ds.Tables.Cast<DataTable>()
.Where
(dt => !columnNames.Except(dt.Columns.Cast<DataColumn>()
.Select(c => c.ColumnName))
.Any()
)
.ToList();

Final Answer (which is not case sensitive):

        List<DataTable> tables =
ds.Tables.Cast<DataTable>()
.Where
(dt => !columnNames.Except(dt.Columns.Cast<DataColumn>()
.Select(c => c.ColumnName), StringComparer.OrdinalIgnoreCase)
.Any()
)
.ToList();

Linq query where first column from DataTable is compared to second column for duplicates

Try following :

            DataTable dt = new DataTable();
dt.Columns.Add("Worknumber",typeof(int));
dt.Columns.Add("Username",typeof(string));

dt.Rows.Add(new object[] {1234, "John"});
dt.Rows.Add(new object[] {1235, "Mike"});
dt.Rows.Add(new object[] {1235, "Mike"});
dt.Rows.Add(new object[] {1236, "Donald"});
dt.Rows.Add(new object[] {1236, "Jack"});

var distinct = dt.AsEnumerable()
.GroupBy(x => x.Field<int>("Worknumber"))
.Select(x => new { workNumber = x.Key, user = x.Select(y => y.Field<string>("Username")).Distinct().ToList() })
.Where(x => x.user.Count > 1)
.Select(x => new { workNumber = x.workNumber, user = string.Join(" ", x.user) })
.ToList();

Linq :DataTable select does not work if column name has space in it?

Use

[Line Number] = '001'

instead



Related Topics



Leave a reply



Submit