Help with querying a DataTable using LINQ?

An approach: a) var result = (from t1 in table.AsEnumerable() join t2 in table.AsEnumerable() on t1. Field(0) equals t2. Field(1) select t1).Distinct() The query above returns IEnumerableField(2) for b) scenario.

An approach: a) var result = (from t1 in table.AsEnumerable() join t2 in table.AsEnumerable() on t1. Field(0) equals t2. Field(1) select t1).Distinct(); The query above returnsIEnumerable.

From this result you can select values of column3 like t2. Field(2) for b) scenario.

– Gerald Torres Aug 24 at 16:36 If you need the output of a query use the value of the result. – Arabela Paslaru Aug 24 at 16:42 Also, I think you meant select t1 there. – Gerald Torres Aug 24 at 16:44 Right.

I've changed the query and added Distinct() at the end. – Arabela Paslaru Aug 24 at 16:51 1 You could do something like var resultInteresct = table.AsEnumerable(). Select(i=>i.

Field(0)). Intersect(table.AsEnumerable(). S?

Elect(j=>j. Field(1))); to get the int values that exists in the columns. After that you can do another query on table to select the Rows with those value.

– Arabela Paslaru Aug 24 at 17:26.

I would create a new class for the three columns. Then create an Iqueryable or List for the new class and add the table rows into that. Then the Linq expression should work.

Class public class myClass { public int column1 { get; set; } public int column2 { get; set; } public stringcolumn3 { get; set; } } Linq a. Return the DataRows whose values in column1 also appears in column2. Var x = (from l1 in myList where (from l2 in myList select l2.

Column2). Contains(l1. Column1) select l1); b.

Return the values of column3 whose values in column1 also appears in column2. Var col3Values = (from l1 in myList where l1. Column2 = l1.

Column3 select l1. Column3).

– Gerald Torres Aug 24 at 16:21 No... Create a class with three properties. List. – Narnian Aug 24 at 16:37.

With help from guys here, and some other sites, I just found out how to actually do be above. This returns the values in column3 whose values in column1 does not appear in column2: from t in table.AsEnumerable() join t2 in table.AsEnumerable(). Select(i => i.

Field(0)). Except(table.AsEnumerable(). Select(j => j.

Field(1))) //the inner Select() returns column1 whose values in it also appears in column2 //I can use either this or the first LINQ I made above //By the way, I said **does not** because I don't think I can use inner join on the opposite of be //unlike the Select() with lambda above; I can just change the Intersect() to Except() :) on t. Field(0) equals t2 where t. Field(1) > 2 //If I need some other condition select t.

Field(2); For c, I made another table: DataTable tableA = new DataTable(); tableA.Columns. Add("columnA", typeof(string)); tableA.Columns. Add("columnB", typeof(string)); tableA.Rows.

Add("apple", "red"); tableA.Rows. Add("banana", "yellow"); tableA.Rows. Add("carrot", "orange"); tableA.Rows.

Add("dog", "commonly brown"); //ok, I can't think of a fruit/vegetable that starts with 'd' right now... tableA.Rows. Add("eggplant", "purple"); And renamed the first table to table1 to avoid/minimize confusion var x = from tA in tableA.AsEnumerable() from t1 in ( from t1 in table1.AsEnumerable() join t2 in ((from t2_1 in table1.AsEnumerable() select t2_1. Field(0)).

Except ((from t2_2 in table1.AsEnumerable() select t2_2. Field(1))).Distinct()) on t1. Field(0) equals t2 where t1.

Field(1) > 2 //extra condition select t1. Field(2)) where tA. Field(0).

StartsWith(t1) select tA; This returns Rows in tableA whose columnA starts with the returned table1's column3, whose column1 values does not appear in column2 and has a value greater than 2 in its column2.

Using the answer in a for another query. Or using something from a LINQ for another LINQ. I have no idea at all how to do something like this.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions