I have a scenario where I do filters depending of checkbox checked, now I have only 2 checkbox and I need to cover all scenarios into if, else conditionals like:
//List Example:
var projectTechnicians = (from DataRow dr in dtEmployeGuid.Rows
where dr["Title"].ToString().Contains("Project Technician")
select new
{
EmpGuid = (Guid)dr["EmpGuid"]
}).ToList();
if (!chkProjectTechs.Checked && !chkTeamLeader.Checked)
{
foreach (DataRowView list in lstTech.SelectedItems)
{
var selectedEmpGuid = (Guid)list[0];
EmpGuid.Add(selectedEmpGuid);
}
parameters = ToDataTable(EmpGuid);
}
else if (!chkTeamLeader.Checked && chkProjectTechs.Checked)
{
foreach (var technician in projectTechnicians)
{
EmpGuid.Add(technician.EmpGuid);
}
parameters = ToDataTable(EmpGuid);
}
else if (!chkProjectTechs.Checked && chkTeamLeader.Checked)
{
foreach (var teamLeader in teamLeaders)
{
EmpGuid.Add(teamLeader.EmpGuid);
}
parameters = ToDataTable(EmpGuid);
}
else if (chkProjectTechs.Checked && chkTeamLeader.Checked)
{
foreach (var technician in projectTechnicians)
{
EmpGuid.Add(technician.EmpGuid);
}
parameters = ToDataTable(EmpGuid);
foreach (var teamLeader in teamLeaders)
{
EmpGuid.Add(teamLeader.EmpGuid);
}
parameters = ToDataTable(EmpGuid);
}
But I need to Add more checkbox, but foreach checkbox I will add to my form I need to add it to each conditional, and at the final of the day I will get a very long code. Is there another way to do this?
There is an issue in you last else if condition. parameters is getting overwritten by teamleaders empGuids
foreach (var technician in projectTechnicians)
{
EmpGuid.Add(technician.EmpGuid);
}
parameters = ToDataTable(EmpGuid);
foreach (var teamLeader in teamLeaders)
{
EmpGuid.Add(teamLeader.EmpGuid);
}
parameters = ToDataTable(EmpGuid); // Overwriting projectTechnicians added above.
You can simplify your code by using if condition only, check my comments in the code.
// add EmpGuids from lstTech if no check box is selected.
// you need to add all checkboxes not selected in the condition
if (!chkProjectTechs.Checked && !chkTeamLeader.Checked)
{
foreach (DataRowView list in lstTech.SelectedItems)
{
var selectedEmpGuid = (Guid)list[0];
EmpGuid.Add(selectedEmpGuid);
}
}
// if projectTechnicians is checked, then add projectTechnicians EmpGuids to parameters
if (chkProjectTechs.Checked)
{
foreach (var technician in projectTechnicians)
{
EmpGuid.Add(technician.EmpGuid);
}
}
// add team leader Empguids if checked.
if (chkTeamLeader.Checked)
{
foreach (var teamLeader in teamLeaders)
{
EmpGuid.Add(teamLeader.EmpGuid);
}
}
// Add new checkbox conditions here
//
// perform your next operation on parameters here.
// At this point you will have either lstTech employees or employees based on checkbox selection in EmpGuid list.
parameters = ToDataTable(EmpGuid);
EDIT
if you are using LINQ, then you can use below LINQ query to add EmpGuids instead of using foreach, for example.
EmpGuid.AddRange(teamLeader.Select(x=>x.EmpGuid));
Related
I have a list inside of another list. I need to add to the list only those elements whose internal list is not empty. The data is pulled from the server. The code contains 4 lists, Each inside of another - Companies - Shops - Campaigns - Coupons. I need to check if the Coupons list is not empty and add the elements related to that. I would be very thankfull if someone could help with that.
foreach (var row in Companies)
{
reportData.companies.Add(new Companies { ID = row.Id, name = row.BrandName });
var Shops = logicService.CompanyShopService.GetShopsByCompany(row.Id);
var Campaigns = logicService.CampaignService.GetCampaignsByCompany(row.Id);
foreach (var shop in Shops)
{
reportData.companies.Where(item => item.ID == shop.ClientID).FirstOrDefault().shops.Add(new Distribution_Shops { Id = shop.Id, Name = shop.Name });
var shop_campaigns = Campaigns.Where(item => item.ShopID == shop.Id);
foreach (var campaign in shop_campaigns)
{
var coupons = logicService.CouponsService.GetAllByCampaign(campaign.Id, false);
foreach (var company in reportData.companies)
{
foreach (var tmp_shop in company.shops)
{
if (tmp_shop.Id == shop.Id)
{
tmp_shop.campaigns.Add(new Distribution_Campaign { Id = campaign.Id, Name = campaign.CampaignName, coupons = coupons});
}
}
}
}
}
}
I have a checkboxlist which I have bind with database in which there are around 9000 items , some of them are selected.I have list in which I have 5000 items.I have to check these 5000 items in checkboxlist and remaining unchecked. Please suggest optimized way.
What I try
foreach (var eachName in Namelist)
{
foreach (ListItem eachCblNameItem in cblName.Items)
{
if (eachCblNameItem.Value == eachName)
{
eachCblNameItem.Selected = true;
}
else
{
eachCblNameItem.Selected = false;
}
}
}
Just an idea as (linq) pseudo-code:
var itemsSelected = from item in checkBoxList.Items
join dbItem in database.SelectedItems
on item.UniqueKey equals dbItem.UniqueKey
select item;
foreach( var item in itemsSelected )
{
item.Selected = true;
}
I try to add item in list view .
But this code like not working at all.
Where did i do wrong ?
btn.Click += (senders, eventArgs) =>
{
foreach (ListViewItem lvis in lvSales.Items)
{
if (lvis.SubItems[0].Text == btn.Text)
{
MessageBox.Show("!!!!!!!");
}
else
{
lvis.Text = count.ToString();
lvis.SubItems.Add(btn.Text);
lvis.SubItems.Add(btn.Name);
lvis.SubItems.Add(count.ToString());
lvis.SubItems.Add(btn.Tag.ToString()); // Email
lvSales.Items.Add(lvis);
count++;
}
}
};
I wan add item to list view.
If the item already added it will add the quantity
else it will add new .
btw when I clicked the button nothing happen .
you cannot add items to the collection you are iterating through with foreach (lvSales.Items). Consider changing foreach to some other loop like 'for(...'
Try this. It is not completely correct but try and fix minor bugs.
var itemFound = false;
foreach (var listViewItem in lvSales.Items)
{
if (listViewItem.SubItems[0].Text == btn.Text)
{
itemFound = true; break;
}
}
if (!itemFound)
{
var newlistViewItem = new ListViewItem();
newlistViewItem.Text = count.ToString();
newlistViewItem.SubItems.Add(btn.Text);
newlistViewItem.SubItems.Add(btn.Name);
newlistViewItem.SubItems.Add(count.ToString());
newlistViewItem.SubItems.Add(btn.Tag.ToString()); // Email
lvSales.Items.Add(lvis);
}
btn.Click += (senders, eventArgs) =>
{
foreach (ListViewItem lvis in lvSales.Items)
{
if (lvis.SubItems[0].Text == btn.Text)
{
//get current quantity of listitem, increment it,
//add the new value to this listitem quantity value...
//keep track of current index, use that to set the new value...
}
else
{
//re instantiate listviewitem, set its values, and add it
}
}
};
I made a survey to register the knowledge inside my organisation. e.g.:
Question:
Microsoft Development;
Subquestions:
SharePoint
CRM
WCF
etc...
People can rate their proficiency using a rating scale. The min value being 0 and the max value 6.
I wan't to retrieve the values from the responses in a new graphical feature.
I'm new to this and this is my code so far:
var thisWeb = SPContext.Current.Web;
foreach (SPList item in thisWeb.Lists)
{
if (item.Title.Contains("Knowledge"))
{
foreach (SPListItem child in item.GetItems())
{
foreach (SPField field in child.Fields)
{
Debug.WriteLine(field.Title);
if (field.TypeAsString == "GridChoice")
{
var ratingscale = field.GetFieldValue(field.);
//var x = ratingscale.GetFieldValue(ratingscale.Choices.ToString());
}
}
}
}
}
}
foreach (SPField field in item.Fields)
{
if (field.Type == SPFieldType.GridChoice)
{
SPFieldRatingScale srsc = (SPFieldRatingScale)field;
Debug.WriteLine(srsc.GetFieldValueAsText(item[field.Title]));
}
}
This will return you values in format below.
question 1;#answer#question 2;#answer# .....
This is what i'm trying to accomplish. I want to select only distinct values from all Rows in Column[0].
Then I want to get all the distinct values from column[2] and group them on column[0].
so basically i got a DataTable like so:
Fruit|Apples
Fruit|Pears
Vegetables|Peas
Vegetables|Carrots
so I want to do a foreach on the distinct values, so I would Enumerate Fruit once and then pick up Apples and Pears, and VegeTables once and pick up Peas and Carrots.
I'm doing this to create Accordion Panes, where I want to group my results under one header, the below code does such, however, it creates two panes of Fruit because it does not realize it already went though fruit.
foreach (DataRow dtrow in dtTable.Rows)
{
string idRow = dtrow[0].ToString();
AccordionPane currentPane = new AccordionPane();
currentPane.ID = "AccordionPane" + Guid.NewGuid().ToString();
currentPane.HeaderContainer.Controls.Add(new LiteralControl(dtrow[0].ToString()));
foreach(DataRow dtRow2 in dtTable.Rows)
{
if(dtRow2[0].ToString() == idRow)
{
currentPane.ContentContainer.Controls.Add(new LiteralControl(dtRow2[1].ToString()));
}
}
NavigateAccordion.Panes.Add(currentPane);
}
You can accomplish this easily when using linq, see for yourself:
var groupedRows = from row in dtTable.Rows.AsEnumerable()
group row by row[0] into grouped
select grouped;
foreach (var group in groupedRows)
{
currentPane = new AccordionPane();
currentPane.HeaderContainer.Controls.Add(group.Key.ToString());
foreach (var row in group)
{
currentPane.ContentContainer.Controls.Add(row[1].ToString());
}
}
Or if you want to stick with your current non-linq approach:
foreach (DataRow dtrow in dtTable.Rows)
{
bool skip = false;
foreach (var pane in NavigateAccordion.Panes)
{
if (pane.HeaderContainer.Controls[0].Text == dtRow[0].ToString())
{
skip = true;
break;
}
}
if (!skip)
{
string idRow = dtrow[0].ToString();
AccordionPane currentPane = new AccordionPane();
currentPane.ID = "AccordionPane" + Guid.NewGuid().ToString();
currentPane.HeaderContainer.Controls.Add(new LiteralControl(dtrow[0].ToString()));
foreach(DataRow dtRow2 in dtTable.Rows)
{
if(dtRow2[0].ToString() == idRow)
{
currentPane.ContentContainer.Controls.Add(new LiteralControl(dtRow2[1].ToString()));
}
}
NavigateAccordion.Panes.Add(currentPane);
}
}