Click here to Skip to main content
15,883,848 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts,

Here is an question where I am worried about. I am newbie to LINQ.
Here is my Datatable
Now I have two radio buttons in which following results should come

on selecting Rbn1, It shows all the Results from Datatble as it was. Like below

Item  Comment
1    Reduced BIN-A
1    Cleared BIN-A

2   Reduced BIN-C
2   Reduced BIN-D
2   Reduced BIN-E

3   Reduced BIN-A1
3   Reduced BIN-A1
3   Cleared BIN-A1
3   Reduced BIN-A2
3   Reduced BIN-A2

4   Cleared BIN-A3
4   Reduced BIN-A4
4   Cleared BIN-A4
4   Reduced BIN-A5


On selecting Rbn2, It should show only the below results

Item  Comment
1    Reduced BIN-A
1    Cleared BIN-A

2   Reduced BIN-C
2   Reduced BIN-D
2   Reduced BIN-E

3   Reduced BIN-A1
3   Reduced BIN-A1
3   Cleared BIN-A1
3   Reduced BIN-A2
3   Reduced BIN-A2

4   Cleared BIN-A3
4   Reduced BIN-A4
4   Cleared BIN-A4
4   Reduced BIN-A5




Meaning in the loop of identifying the cleared bin and reduced Bin , It should show only those where the bin is cleared.

I hope I am not confusing.

Please help me with LINQ query. Or if there is any other easy alternative is also great.

Thanks
Posted
Updated 14-Oct-15 8:19am
v2
Comments
sudevsu 19-Oct-15 14:50pm    
Can you have Right outer Join for this?

As to your requirements:

VB
'create example DataTable
Dim dt AS DataTable = New DataTable()
'add columns
dt.Columns.Add(New DataColumn("Item", Type.GetType("System.Int32")))
dt.Columns.Add(New DataColumn("Comment", Type.GetType("System.String")))
'add rows
dt.Rows.Add(New Object(){1, "Reduced BIN-A"})
dt.Rows.Add(New Object(){1, "Cleared BIN-A"})
dt.Rows.Add(New Object(){2, "Reduced BIN-C"})
dt.Rows.Add(New Object(){2, "Reduced BIN-D"})
dt.Rows.Add(New Object(){2, "Reduced BIN-E"})
dt.Rows.Add(New Object(){3, "Reduced BIN-A1"})
dt.Rows.Add(New Object(){3, "Reduced BIN-A1"})
dt.Rows.Add(New Object(){3, "Cleared BIN-A1"})
dt.Rows.Add(New Object(){3, "Reduced BIN-A2"})
dt.Rows.Add(New Object(){3, "Reduced BIN-A2"})
dt.Rows.Add(New Object(){4, "Cleared BIN-A3"})
dt.Rows.Add(New Object(){4, "Reduced BIN-A4"})
dt.Rows.Add(New Object(){4, "Cleared BIN-A4"})
dt.Rows.Add(New Object(){4, "Reduced BIN-A5"})


Dim clearedBins = dt.AsEnumerable() _
        .Select(Function(c) New With _
            { _
                Key .Item = c.Field(Of Integer)("Item"), _
                Key .Action = c.Field(Of String)("Comment").Substring(0, c.Field(Of String)("Comment").IndexOf(" ")), _
                Key .Bin = c.Field(Of String)("Comment").Substring(c.Field(Of String)("Comment").IndexOf(" "), _
                        (c.Field(Of String)("Comment").Length - c.Field(Of String)("Comment").IndexOf(" "))) _
            }) _
        .GroupBy(Function(a) New With{Key .Bin=a.Bin, Key .Item = a.Item}) _
        .Select(Function(g) New With _
            { _
                Key .Bin = g.Key.Bin, _
                Key .Item = g.Key.Item, _
                Key .Action = String.Join(",", g.Select(Function(x) x.Action)) _
            }) _
        .Where(Function(b) b.Action.Contains("Cleared")) _
        .ToList()


C#
//create example DataTable
DataTable dt = new DataTable();
//add columns
dt.Columns.Add(new DataColumn("Item", typeof(int)));
dt.Columns.Add(new DataColumn("Comment", typeof(string)));
//add rows
dt.Rows.Add(new Object[]{1, "Reduced BIN-A"});
dt.Rows.Add(new Object[]{1, "Cleared BIN-A"});
dt.Rows.Add(new Object[]{2, "Reduced BIN-C"});
dt.Rows.Add(new Object[]{2, "Reduced BIN-D"});
dt.Rows.Add(new Object[]{2, "Reduced BIN-E"});
dt.Rows.Add(new Object[]{3, "Reduced BIN-A1"});
dt.Rows.Add(new Object[]{3, "Reduced BIN-A1"});
dt.Rows.Add(new Object[]{3, "Cleared BIN-A1"});
dt.Rows.Add(new Object[]{3, "Reduced BIN-A2"});
dt.Rows.Add(new Object[]{3, "Reduced BIN-A2"});
dt.Rows.Add(new Object[]{4, "Cleared BIN-A3"});
dt.Rows.Add(new Object[]{4, "Reduced BIN-A4"});
dt.Rows.Add(new Object[]{4, "Cleared BIN-A4"});
dt.Rows.Add(new Object[]{4, "Reduced BIN-A5"});


var clearedBins = dt.AsEnumerable()
		.Select(c=>new
			{
				Item = c.Field<int>("Item"),
				Action = c.Field<string>("Comment").Substring(0, c.Field<string>("Comment").IndexOf(" ")),
				Bin = c.Field<string>("Comment").Substring(c.Field<string>("Comment").IndexOf(" "), (c.Field<string>("Comment").Length - c.Field<string>("Comment").IndexOf(" ")))
			})
		.GroupBy(a=>new {a.Bin, a.Item})
		.Select(g=>new
			{
				Bin = g.Key.Bin,
				Item = g.Key.Item,
				Action = string.Join(",", g.Select(x=>x.Action))
			})
 		.Where(b=>b.Action.Contains("Cleared"))
		.ToList();

Result:

Bin    Item Action
BIN-A  1    Reduced,Cleared 
BIN-A1 3    Reduced,Reduced,Cleared 
BIN-A3 4    Cleared 
BIN-A4 4    Reduced,Cleared 


Feel free to change it to your needs.
 
Share this answer
 
v4
Comments
sudevsu 15-Oct-15 14:08pm    
Can you please tell me in vb.net? I want to test this, I tried and it shows errors

Dim clearedBins = dtNew.AsEnumerable().[Select](Function(c) New With { _
Key .Item = c.Field(Of Integer)("Item"), Key .Action = c.Field(Of String)("Comment").Substring(0, c.Field(Of String)("Comment").IndexOf(" ")), _
Key .Bin = c.Field(Of String)("Comment").Substring(c.Field(Of String)("Comment").IndexOf(" "), (c.Field(Of String)("Comment").Length - c.Field(Of String)("Comment").IndexOf(" ")))}).GroupBy(Function(a) New From { a.Bin, a.Item }).Select(Function(g) New With { Key .Bin = g.Key.Bin, Key .Item = g.Key.Item, Key .Action = String.Join(",", g.[Select](Function(x) x.Action)) }).Where(Function(b) b.Action.Contains("Cleared")).ToList()
Maciej Los 16-Oct-15 6:09am    
OK, i'll show you an example in vb.net. Please, wait...
Maciej Los 16-Oct-15 11:57am    
Done! Please, see updated answer.
sudevsu 19-Oct-15 8:38am    
Thank you
Maciej Los 19-Oct-15 9:17am    
You're very welcome. Can you accept my answer as a solution (green button) - formally to remove your question from unanswered list?
Try with below code:
C#
DataTable table1 = new DataTable();
// Add Data Row to table1 object

var itemList = (from r in table1.AsEnumerable()
			  where r.Field<string>("Comment").Contains("Cleared BIN")
			  select new
			  {
				  item = r.Field<int>("Item")
				  comment = r.Field<string>("Comment")
			  }).ToList();
			  
foreach (var tempRec in itemList)
{
	int tempItem = tempRec.item;
	string commentStr = tempRec.comment;
}

Here I used only two columns(Item, Comment) as per your question. you can change/add if you have more columns. Secondly I assumed that Item column is int and Commnet column is string if it is other than that you need to modify.
 
Share this answer
 
v3
Comments
sudevsu 14-Oct-15 14:34pm    
Hey Manas,

You misunderstand the question, I want those rows that are associated with same bin if it reduced and cleared. Not just cleared alone. If the next bin is not cleared then I don't want that row.
sudevsu 14-Oct-15 14:35pm    
Please see the example where I strikes out the lines that shudn't be considered
Maciej Los 14-Oct-15 16:27pm    
+4! Quite good sample as to the OP's requirements, which wasn't clear enough!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900