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
Iterate each
DataRow
and get the value fromDataColumn
.Add value into array/list.
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
Working with System.Linq to get the email as
List<string>
.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
Regex to Match Alphanumeric and Spaces
Count() a Specfic Attribute Within a List C#
Split a Comma-Separated String With Both Quoted and Unquoted Strings
Formatting Date in Linq-To-Entities Query Causes Exception
Smtpexception: Unable to Read Data from the Transport Connection: Net_Io_Connectionclosed
Check If a File Is Real or a Symbolic Link
How to Set Shadow Effect on Imageview
Could Not Parse the Json File ,Error in Progam.Cs ASP.NET Core
Asp.Net Core 2 Web API Timeout Issue
How to Compare Key/Value Dictionary With == Operator on a Ireadonlycollection<String>
Using X-Alt-Desc/Applying HTML to Calendar Invites in Outlook
Regular Expression for Valid Filename
How to Check If Multiple Values in an Array Are Equal to One String
Empty String Not Being Converted to Null When Passing Json Object to Controller
How to Open a Url in Chrome Incognito Mode
How to Use a Class from One C# Project With Another C# Project
Using Recursion to Add Odd Numbers
How to Open the Default Chrome Profile Through Selenium, Chromedriver and Googlechrome