select values from DataTable column where not in a list - c#

Here is my code :
List<long> LevelsArray = CardServiceDetailsDt.AsEnumerable()
.Select(r => r.Field<long("LEVEL_ID"))
.ToList<long>();
DataTable leveldt =ClientsDataHelper.GetServicePackagesLevel(new WebSiteDataHelper()
.SelectedStoreID.Value);
how can I select the LEVEL_ID from which are not in the list ?

Asssuming that the second table also has the column LEVEL_ID:
IEnumerable<DataRow> filteredRows = leveldt.AsEnumerable()
.Where(r => !LevelsArray.Contains(r.Field<long>("LEVEL_ID")));
Here's a probably more efficient Linq Left-Outer-Join appproach
filteredRows = from row in leveldt.AsEnumerable()
join levelID in LevelsArray
on row.Field<long>("LEVEL_ID") equals levelID into rowLevelID
from subrow in rowLevelID.DefaultIfEmpty()
where subrow == null
select row;
If you only need the ID's which are not in the list, use Enumerable.Except:
List<long> notInList = leveldt.AsEnumerable()
.Select(r => r.Field<long>("LEVEL_ID"))
.Except(LevelsArray)
.ToList();
which is the most efficient approach since it's using a set. It select only the ID's from the datarows which are not in the array.

List<long> LevelsArray = (from r in leveldt.AsEnumerable()
let LEVEL_ID = r.Field<long>("LEVEL_ID")
where !list.Contains(LEVEL_ID)
select LEVEL_ID).ToList<long>();

Related

List<T> joins DataTable

I have a List of objects (lst) and DataTable (dt). I want to join the lst and dt on the common field (code as string) and need to return all matching rows in the lst.
My List contains two columns i.e code and name along with values below:
code name
==== ====
1 x
2 y
3 z
The DataTable contains two columns i.e code and value along with values below:
code value
==== =====
3 a
4 b
5 c
The result is:
3 z
Below is my code; but I know it is not a correct statement and thus seeking your advice here. I would be much appreciated if you could guide me on how to write the correct statement.
var ld = from l in lst
join d in dt.AsEnumerable() on l.code equals d.code
select new { l.code, l.name };
You can use Linq query or Join extension method to join the collection on code. Just that when you select data from datatable, you need to use dt.Field method. Please use either of the following code.
Query1:
var ld = lst.Join(dt.AsEnumerable(),
l => l.code,
d => d.Field<string>("code"),
(l, d) => new
{
l.code,
l.name,
value = d.Field<string>("value")
}).ToList();
Query2:
var ld = (from l in lst
join d in dt.AsEnumerable()
on l.code equals d.Field<string>("code")
select new
{
l.code,
l.name,
value = d.Field<string>("value")
}).ToList();
Query3:
var ld = (from l in lst
join d in dt.AsEnumerable()
on l.code equals d.Field<string>("code")
let value = d.Field<string>("value")
select new
{
l.code,
l.name,
value
}).ToList();
You can try any of the below.
var ld = from l in lst
join d in dt.AsEnumerable() on l.code equals d.Field<int>("code")
select new { l.code, l.name };
var ld = lst.Join(dt.AsEnumerable(), l => l.code, d => d.Field<int>("code"), (l,d) => new { l.code, l.name });
It's not clear what your required output is but it looks as if you are correctly getting the only common records. You could extend your select to
select new { l.code, l.name, d.value }
Which would give all the data/columns from both tables.
code name value
==== ==== =====
3 z a
Try this:
var ld = from l in lst
join d in dt.Cast <DataRow>() on l.code equals d["code"].ToString()
select new { l.code, l.name };
SO you have a List and a DataTable. You don't plan to use the Values of the DataTable, only the Codes.
You want to keep those List items, that have a Code that is also a code in the DataTable.
If you plan to use your DataTable for other things than just for this problem, My advice would be to first create a procedure to convert your DataTable into an enumerable sequence.
This way you can add LINQ statements, not only for this problem, but also for other problems.
Let's create an extension method for your DataTable that converts the data into the items that are in the DataTable. See extension methods demystified.
Alas, I don't know what's in your DataTable, let's assume that your DataTable contains Orders
class CustomerOrder
{
public int Id {get; set;}
public int CustomerId {get; set;}
public int Code {get; set;}
public string Value {get; set;}
...
}
The extension method that extends functionality of class DataTable:
public static IEnumerable<Order> ToCustomerOrders(this DataTable table)
{
return table.AsEnumerable().Select(row => new CustomerOrder
{
Id = ...
CustomerId = ...
Code = ...
Value = ...
};
}
I'm not really familiar with DataTables, but you know how to convert the cells of the row into the proper value.
Usage:
DataTable table = ...
Int customerId = 14;
var ordersOfThisCustomer = table.ToCustomerOrders
.Where(customerOrder => customerOrder.CustomerId == customerId)
.FirstOrDefault();
In words: convert the datatable into CustomerOrders, row by row, and check for every converted CustomerOrder whether it has a CustomerId equal to 14. Stop if found. return null if there is no such row.
Now that you've got a nice reusable procedure that is also easy to test, debug and change, we can answer your question.
Given a DataTable with CustomerOrders, and a sequence of items that contain Code and Name, keep only those items from the sequence that have a Code that is also a Code in the DataTable.
var dataTable = ... // your DataTable, filled with CustomerOrders.
var codeNames = ... // your list with Codes and Names
var codesInDataTable = dataTable.ToCustomerOrders
.Select(customerOrder => customerOrder.Code)
.Distinct();
This will create an enumerable sequence that will convert your DataTable row by row and extract property Code. Duplicate Code values will be removed.
If Codes are unique, you don't need Distinct.
Note: the enumerable sequence is not enumerated yet!
var result = codeNames
.Where(codeName => codesInDataTable.Contains(codeName.Code))
.ToList();
In words: for every [Code, Name] combination in your list, keep only those [Code, Name] combinations that have a value for Code that is also in codesInDataTable.

How to convert to int and then compare in linq query c#

IEnumerable<classB> list = getItems();
//dt is datatable
list = list.Where(x => Convert.ToInt32( !dt.Columns["Id"]) == (x.Id));
I want to only keep the items in the list which match in datatable id column. The rest are removed. I m not doing it right.
The datatable can have: ID - 1,3,4,5,7
The list can have: ID - 1,2,3,4,5,6,7,8,9,10
I want the output list to have: ID - 1,3,4,5,7
Your code won't work because you're comparing a definition of a column to an integer value. That's not a sensible comparison to make.
What you can do is put all of the values from the data table into a collection that can be effectively searched and then get all of the items in the list that are also in that collection:
var ids = new HashSet<int>(dt.AsEnumerable()
.Select(row => row.Field<int>("Id"));
list = list.Where(x => ids.Contains(x.Id));
Try this one
var idList = dt.AsEnumerable().Select(d => (int) d["Id"]).ToList();
list = list.Where(x => idList.Contains(x.Id));
You can't do it like that. Your dt.Columns["Id"] returns the DataColumn and not the value inside that column in a specific datarow. You need to make a join between two linq query, the first one you already have, the other you need to get from the DataTable.
var queryDt = (from dtRow in dt
where !dtRow.IsNull("Id")
select int.Parse(dtRow["Id"])).ToList();
Now the join
var qry = from nonNull in queryDt
join existing in list on nonNull equals list.id

C# - Remove rows with the same column value from a DataTable

I have a DataTable which looks like this:
ID Name DateBirth
.......................
1 aa 1.1.11
2 bb 2.3.11
2 cc 1.2.12
3 cd 2.3.12
Which is the fastest way to remove the rows with the same ID, to get something like this (keep the first occurrence, delete the next ones):
ID Name DateBirth
.......................
1 aa 1.1.11
2 bb 2.3.11
3 cd 2.3.12
I don't want to double pass the table rows, because the row number is big.
I want to use some LinQ if possible, but I guess it will be a big query and I have to use a comparer.
You can use LINQ to DataTable, to distinct based on column ID, you can group by on this column, then do select first:
var result = dt.AsEnumerable()
.GroupBy(r => r.Field<int>("ID"))
.Select(g => g.First())
.CopyToDataTable();
I was solving the same situation and found it quite interesting and would like to share my finding.
If rows are to be distinct based on ALL COLUMNS.
DataTable newDatatable = dt.DefaultView.ToTable(true, "ID", "Name", "DateBirth");
The columns you mention here, only those will be returned back in newDatatable.
If distinct based on one column and column type is int then I would prefer LINQ query.
DataTable newDatatable = dt.AsEnumerable()
.GroupBy(dr => dr.Field<int>("ID"))
.Select(dg => dg).Take(1)
.CopyToDataTable();
If distinct based on one column and column type is string then I would prefer loop.
List<string> toExclude = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
var idValue = (string)dt.Rows[i]["ID"];
if (toExclude.Contains(idValue))
{
dt.Rows.Remove(dt.Rows[i]);
i--;
}
toExclude.Add(glAccount);
}
Third being my favorite.
I may have answered few things which are not asked in the question. It was done in good intent and with little excitement as well.
Hope it helps.
you can try this
DataTable uniqueCols = dt.DefaultView.ToTable(true, "ID");
Not necessarily the most efficient approach, but maybe the most readable:
table = table.AsEnumerable()
.GroupBy(row => row.Field<int>("ID"))
.Select(rowGroup => rowGroup.First())
.CopyToDataTable();
Linq is also more powerful. For example, if you want to change the logic and not select the first (arbitrary) row of each id-group but the last according to DateBirth:
table = table.AsEnumerable()
.GroupBy(row => row.Field<int>("ID"))
.Select(rowGroup => rowGroup
.OrderByDescending(r => r.Field<DateTime>("DateBirth"))
.First())
.CopyToDataTable();
Get a record count for each ID
var rowsToDelete =
(from row in dataTable.AsEnumerable()
group row by row.ID into g
where g.Count() > 1
Determine which record to keep (don't know your criteria; I will just sort by DoB then Name and keep first record) and select the rest
select g.OrderBy( dr => dr.Field<DateTime>( "DateBirth" ) ).ThenBy( dr => dr.Field<string>( "Name" ) ).Skip(1))
Flatten
.SelectMany( g => g );
Delete rows
rowsToDelete.ForEach( dr => dr.Delete() );
Accept changes
dataTable.AcceptChanges();
Heres a way to achive this,
All you need to use moreLinq library use its function DistinctBy
Code:
protected void Page_Load(object sender, EventArgs e)
{
var DistinctByIdColumn = getDT2().AsEnumerable()
.DistinctBy(
row => new { Id = row["Id"] });
DataTable dtDistinctByIdColumn = DistinctByIdColumn.CopyToDataTable();
}
public DataTable getDT2()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Dob", typeof(string));
dt.Rows.Add("1", "aa","1.1.11");
dt.Rows.Add("2", "bb","2.3.11");
dt.Rows.Add("2", "cc","1.2.12");
dt.Rows.Add("3", "cd","2.3.12");
return dt;
}
OutPut: As what you expected
For moreLinq sample code view my blog

C#, lambda, linq

Say I have an int list that contains a list of ids. I have a linq table and I want to return a particular column but only where the ID of the linq table is equal to any of the ID's in the int list.
So far I have:
dc.tb_References.SelectMany(n => n.ID == ids).ToList();
In sql I would just write:
SELECT Column_Name from Table where ID in (1,2,3,4)
I have been googling but I can't find what I'm looking for. Does anyone have any tips? I would like to stick with lambda expressions.
You can use Contains() method on ID list.
dc.tb_References.Where(item => ids.Contains(item.ID)).ToList();
Try this
dc.tb_References.Where(n => ids.Contains(n.ID)).ToList();
Use the Where method with the Contains method:
dc.tb_References
.Where(n => theListOfIds.Contains(n.ID))
.Select(x => x.Column_Name)
.ToList();
or you can do:
var query = from item in dc.tb_References
where theListOfIds.Contains(item.ID)
select item.Column_Name;
var list = query.ToList();
SelectMany is used to select items from a sub-list and then retun all these ites as a list:
Fruit.Items: Apple, Pear
Veggies.Items: Carrot, Cabbage
List.Items: Fruit, Veggies
List.Items.SelectMany(x => x.Items)
Result:
Apple, Pear, Carrot, Cabbage
Is this the kind of thing you're after?
int[] myIds = {1,4,5,3};
List<int> list = new List<int>();
list.Add(1);
list.Add(5);
list.Add(8);
list.Add(9);
list.Add(10);
list.Add(12);
List<int> select = (from l in list where myIds.Contains(l) select l).ToList();
To generate IN clause you need to call the Contains method on the collection and pass that method the property of the object you want to search for:
var ids = new int[] { 1, 3 };
var query = from n in dc.tb_References
where ids.Contains(n.ID)
select n;
Here is generated SQL (from LinqPad):
DECLARE #p0 Int = 1
DECLARE #p1 Int = 3
SELECT [t0].[ID], [t0].[Foo], [t0].[Bar]
FROM [tb_References] AS [t0]
WHERE [t0].[ID] IN (#p0, #p1)
int ids = new int[]{1,2,3,4};
var list = (from d in dc.tb_References
where ids. Contains(d.ID)
select d. Column_Name).ToList();

LINQ to DataSet to get generic list from DataTable

DataTable table = DataProvider.GetTable()
var clientIds = from r in table.AsEnumerable()
select r.Field<string>("CLIENT_ID");
I want clientIds to be a List<string>. Currently it's an EnumerableRowCollection<>
What am I missing?
this may work
DataTable table = DataProvider.GetTable()
var clientIds = (from r in table.AsEnumerable()
select r.Field<string>("CLIENT_ID")).ToList();
Here is one way to do it:
var clientIds = table.Rows.Cast<DataRow>().Select(r => r.Field<string>("CLIENT_ID").ToList();
Or, if this syntax is working but not bringing back the results as a list, you can do something like:
var clientIds = (from r in table.AsEnumerable()
select r.Field<string>("CLIENT_ID")).ToList();

Categories

Resources