I want to select values from LINQ
but these values depend on another control in my windows form in C#
Here is my code
var lstInfo = grdBreakDown.Rows.Cast<DataGridViewRow>()
.Where(x => !x.IsNewRow)
.Select(x => (tag: x.Cells["tag"].Value.ToString(), Scheme: x.Cells["Scheme"].Value.ToString(), Value: x.Cells["Value"].Value.ToString()))
.Distinct()
.ToList();
lvwInfo.Items.AddRange( lstInfo
.Select(x => new ListViewItem(new string[] { x.tag, x.Scheme, x.Value }))
.ToArray()
);
This code works perfectly and displays all Schemes something like this
Tag Scheme Value
Red Master 88
Red Master 14
Red Admin 39
Red Admin 31
Blue Master 91
Blue Master 10
Blue Admin 54
Blue Admin 04
But I want to make a few changes and only displays Schemes & Value when a checkbox in the windows form is ticked and if not ticked just display empty string
something like this
.Select(x => (
tag: x.Cells["tag"].Value.ToString(),
Scheme:
if(
(!cbxMaster.checked && x.Cells["Scheme"].Value.ToString() == "Master")
||
(!cbxAdmin.checked && x.Cells["Scheme"].Value.ToString() == "Admin"), "", x.Cells["Scheme"].Value.ToString()
),
Value:
if((!cbxMaster.checked && x.Cells["Scheme"].Value.ToString() == "Master")
||
(!cbxAdmin.checked && x.Cells["Scheme"].Value.ToString() == "Admin"), "", x.Cells["Value"].Value.ToString()
)
)
So if Master ticked an Admin is not ticked
output will be like this
Tag Scheme Value
Red Master 88
Red Master 14
Red
Blue Master 91
Blue Master 10
Blue
but this is not working.
how to do that with linq?
CodePudding user response:
To modify what you have selected, you have to browse through the entire selection.
var lstInfo = grdBreakDown.Rows.Cast<DataGridViewRow>()
.Where(x => !x.IsNewRow)
.Select(x => (tag: x.Cells["tag"].Value.ToString(), Scheme: x.Cells["Scheme"].Value.ToString(), Value: x.Cells["Value"].Value.ToString()))
.Distinct()
.ToList()
.foreach (elem=>{
if(elem.Sheme.equals("Admin")){
elem.Sheme="";
elem.value="";}
});
CodePudding user response:
Why not try a conditional operator for the value you are searching for?
.Where(x => x.Cells["Scheme"].Value.ToString() == ((checked) ? "Master" : "Admin") )
Checked could be a bollean value or whether the tickbox is ticked. I think this will work