The code below works successfully to remove if a value exists in a list. How do I add a where clause such that only for list items where sType = "File"
MyGlobals.lstNewItems.RemoveAll(item => item.sItemName == rows[i].Cells[0].Value.ToString());
Pseudo Code for what i want
MyGlobals.lstNewItems.Where(y => y.sType == "File").RemoveAll(item => item.sItemName == rows[i].Cells[0].Value.ToString());
If you want to remove all of the items where both conditions are true, then simply AND them together:
MyGlobals.lstNewItems.RemoveAll(item =>
item.sItemName == rows[i].Cells[0].Value.ToString()
&& item.sType == "File");
Related
I need to remove a particular value from the list based on the condition
skillResult.Where(search =>
resrictedskills.Any(resrictedskill =>
search.L5_Id.ToString() == resrictedskill.CRTS_SKILLID.ToString()
&& resrictedskill.CRTS_SKILLTYPE.ToLower() == "primary"
)
).ToList()
.ForEach(skill => skill.isRestrictedpri = true);
I tried using remove but I am getting error "cannot convert from void to particular list model"
skillResult.Remove(
skillResult.Where(search =>
resrictedskills.Any(resrictedskill =>
search.L5_Id.ToString() == resrictedskill.CRTS_SKILLID.ToString()
&& resrictedskill.CRTS_SKILLTYPE.ToLower() == "primary"
)
).ToList()
.ForEach(skill => skill.isRestrictedpri = true)
);
Remove() expects one item and returns a bool telling whether the item was found and could be removed.
You must use RemoveAll which expects a condition and returns and int telling how many items have been removed.
Since none of the two methods return skill objects, you cannot chain the ForEach on them to set a property of the skills. Therefore, you need two statements.
skillResult.RemoveAll(skillResult => resrictedskills
.Any(restrictedSkill =>
skillResult.L5_Id == restrictedSkill.CRTS_SKILLID &&
restrictedSkill.CRTS_SKILLTYPE.ToLower() == "primary"
)
);
skillResult.ForEach(skill => skill.isRestrictedpri = true);
I have the following two tables which hold the information on items that have been completed I needed to do it this way for reporting purposes.
qry = db.AssemblyListItems
.AsNoTracking()
.Where(x => x.ProductionPlanID == (long)_currentPlan.ProductionPlan )
.ToList();
var _query = qry.Where(w => w.ItemCode == "EPR15CT.L01" && w.DocumentNo == "0000026590")
.SingleOrDefault();
var hasbeenAssembled = dbCompletedPrinteds
.AsNoTracking()
.Where(x => x.ProductionPlanId == (long)_currentPlan.ProductionPlan)
.ToList();
foreach (var item in hasbeenAssembled) {
qry.RemoveAll(X => X.SOPOrderReturnID == Int32.Parse(item.SopLineItemId) );
}
If it finds any matching items in the second table to remove it from the main query.
You will see the tables have much the same data stored in them. But for some reason the the items is still showing in the I need some way of looping the first query with the second query and removing the matching items from the qry object.
So steps I need to do is :
Loop completed and printed object remove any matching products with the same document number and item code and match the productplan id item and then remove it from the master AssemblyListItems query and then dispaly in a gui at the min its keeping the item in the list.
Edit 2
This would work but I dont think its very effiecent.
List<AssemblyListItems> _query = qry.ToList();
foreach (AssemblyListItems item in _query)
{
var hasbeenAssembled = db.CompletedPrinteds.AsNoTracking().Where(x => x.ProductionPlanId == item.ProductionPlanID).ToList();
foreach(var subitem in hasbeenAssembled )
{
if(item.ProductionPlanID ==subitem.ProductionPlanId && item.DocumentNo == subitem.DocumentNo && item.DocumentNo == subitem.DocumentNo)
{
qry.RemoveAll(x => x.ProductionPlanID == subitem.ProductionPlanId && x.DocumentNo == item.DocumentNo && x.ItemCode == subitem.StockCode);
}
}
}
Edit 3
To Show the items in the edmx
Last week I did query below using Left outer Join to get three group of data
var results = (from srs in srsEmps
join dest in destEmps on srs.EmpCode equals dest.EmpCode into dsNull
from dest in dsNull.DefaultIfEmpty()
select new { srs = srs, dest = dest }).ToList();
var Common = results.Where(x => (x.srs != null) && ( x.dest != null)).ToList();
var Deleted = results.Where(x => x.dest != null).ToList();
var NewlyAdded = results.Where(x => x.srs != null);
Something like this maybe?
//first get list of assembled/completed items with the _currentplan's ID:
var hasbeenAssembled =
dbCompletedPrinteds
.AsNoTracking()
.Where(x => x.ProductionPlanId == (long)_currentPlan.ProductionPlan)
//note: not sure of underlying DB technology here, but this .ToList() will
//typically cause a DB query to execute here.
.ToList();
//next, use that to filter the main query.
qry = db.AssemblyListItems
.AsNoTracking()
.Where(x =>
//Get current plan items
(x.ProductionPlanID == (long)_currentPlan.ProductionPlan)
//filter out items which are in the previous list of 'completed' ones
&& (!hasBeenAssembled.Any(hba => hba.SopLineItemId==x.SOPOrderReturnID))
)
.ToList();
//I don't have any idea what _query is for, it doesn't seem to be used for anything
//in this example...
var _query = qry.Where(w => w.ItemCode == "EPR15CT.L01" && w.DocumentNo == "0000026590")
.SingleOrDefault();
I have problem to understend what is happening in foreach loop- listOfBookedTimes successfuly gets elements that I want, but after execution of the next line listOfBookedTimes is empty. Why? (All lists contain DateTime)
foreach (var day in allDays)
{
list = Rep.GetListOfWorkingHours(fullDayWorkingHours, day, sPWorkingHours);
bookedTimes = _bookingsService.GetBookedTimes(day, providerId);
foreach (var b in bookedTimes)
{
var listOfBookedTimes = list.Where(m => m.TimeOfDay == (b.TimeOfAppointment.TimeOfDay));
list.RemoveAll(m => m.TimeOfDay == (b.TimeOfAppointment.TimeOfDay));
listOfBookedTimes.Select(m => m.Year - 50);
list.AddRange(listOfBookedTimes);
}
You are removing all elements from the list in this statement
list.RemoveAll(m => m.TimeOfDay == (b.TimeOfAppointment.TimeOfDay));
notice that
listOfBookedTimes = list.Where(m => m.TimeOfDay == (b.TimeOfAppointment.TimeOfDay));
and
list.RemoveAll(m => m.TimeOfDay == (b.TimeOfAppointment.TimeOfDay));
have the same condition
You can execute ToList() in order to get a new copy of the list, so that removing items from the original list won't affect it:
listOfBookedTimes = list.Where(m => m.TimeOfDay == (b.TimeOfAppointment.TimeOfDay)).ToList();
BUT
listOfBookedTimes still holds the references to the original items from the list so while adding removing elements to both those lists won't affect each other, modifying properties of single item that is contained by both lists will be applied to both of them.
Your problem is not the RemoveAll but rather the fundemental understanding of LinQ and yield return.
When you call
list.Where(m => m.TimeOfDay == (b.TimeOfAppointment.TimeOfDay));
it is not executed but rather returns an enumerator that will filter the collection on iteration. In the next line you remove all entries you wanted to fetch in the previous line.
When you finally iterate the collection in
list.AddRange(listOfBookedTimes);
it is already empty.
Solution: Add .ToArray() or .ToList() after the Where and it should work as expected. Like this:
var listOfBookedItems = list.Where(m => m.TimeOfDay == (b.TimeOfAppointment.TimeOfDay))
.ToList();
I want to do something like this:
var list = db.Respaldoes.Where(x => x.type == "Backup");
if (condicion1)
list.Where(x => x.entity == "GIELCJLT03");
if (condicion2)
list.Where(x => x.activity_type == "0");
I don't know if something like this is possible, I get the list but the filters are never applied.
You could try something like this:
var list = db.Respaldoes.Where(x => x.type == "Backup");
if (condicion1)
list = list.Where(x => x.entity == "GIELCJLT03");
if (condicion2)
list = list.Where(x => x.activity_type == "0");
Initially the list when the Where clause, x => x.type == "Backup", will be executed it will give you the initial list you refer to. Then, if the condicion1 is true, you will make a second fitering and you will assign the result to list. There again you have deffered execution. Only when list will be requested to be consumed by another piece of your code will be executed -hence the name deffered execution. The same holds for the second condicion and so on.
Where returns an IEnumerable<T>, which defers the execution until a later action forces the query to be resolved. If you want to resolve your query, immediately execute it with ToList().
In addition, you are not actually doing anything with your filtered list. By reassigning the filtered list to your original list object, it will update the collection with the filter, removing objects that do not match your predicate.
var list = db.Respaldoes.Where(x => x.type == "Backup");
if (condicion1)
list = list.Where(x => x.entity == "GIELCJLT03").ToList();
if (condicion2)
list = list.Where(x => x.activity_type == "0").ToList();
I have the following linq:
objfl = db.tblFl.First(t => t.sp == id && t.ProgID == sPgm);
I like to also order by id but not sure how to do this. I tried a number of different ways but was not successful
As suggested by BrokenGlass, if you want to filter by ProgID, sort by sp and retrieve the first item:
db.tblFl.Where(t => t.ProgID == sPgm)
.OrderBy(t => t.sp)
.First()
Try this
objfl = db.tblFl.Where(t => t.sp == id && t.ProgID == sPgm).OrderBy(t => t.sp);