Is there a simple solution to remove specific itemS from ListView.SelectedItems?
I've a ListView bound to an ObservableCollection<MyClass> (MyClass has some attributes e.g. Name).
Something like:
mylistview.SelectedItems.Remove(FROM myClassItem IN mylistview.SelectedItems WHERE myClassItem.Name == "test");
Of course, it doesn't work.
foreach (var item in mylistview.SelectedItems
.Cast<ListViewItem>()
.Where(lvi => lvi.Name == "test")
item.Remove();
Related
I have an listview, i change some item.backcolor is orange.
So now i have an method and i want to be like this:
foreach (ListViewItem item in listView1.BackColor.Orange)
item.BackColor = listView1.BackColor;
The part (BackColor.Orange) is absolutely wrong.
Can foreach method can be written like that some how?
I know that code can write like this but i dont want:
foreach (ListViewItem item in listView1.Items))
if (item.BackColor == Color.Orange)
item.BackColor = listView1.BackColor;
Yes you can with Linq using Where:
foreach (var item in listView1
.Items
.Cast<ListViewItem>()
.Where(i => i.BackColor == Color))
You can write it like this:
foreach(var item in listView1.Items.Where(x => x.BackColor == Color.Orange))
{
//your code
}
I have this code to modify the categorie list foreach opened Form
var lst = Application.OpenForms.OfType<FrmProduit>();
foreach (var item in lst)
{
item.getCategorie(Categories.getLastCategorieId());
}
Well, the first loop is ok, but then second loop I have the InvalidOperationExcetion
Any help please
Try using the List<T> ForEach method as follows:
Application.OpenForms.OfType<FrmProduit>()
.ToList()
.ForEach(i => i.getCategorie(Categories.getLastCategorieId()));
This will not throw the error - but if the 'getCategorie' method has a return value, you might want to do something with it. If that's the case, you need:
Application.OpenForms.OfType<FrmProduit>()
.ToList()
.ForEach(i =>
{
var retVal = i.getCategorie(Categories.getLastCategorieId());
// do something with retVal here...
});
I'm trying to show my items in pagination method.
the first part happened by grouping them by i don't know how can sort them too .
in code Item.page contain each item page number and Item.priority contain
order of item.
now how can i sort by order too ?
this is my code :
"#model IEnumerable<MyProgram.Models.Question>"
foreach (var group in Model.GroupBy((item => item.Page)))
{
<fieldset>
<legend></legend>
#{
foreach (var item in group)
{
if (item.Type == "label")
{
i tired this but nothing came up after item. :
foreach (var group in Model.GroupBy((item => item.Page)).OrderBy(item => item.))
This shall work:
Model.GroupBy(item=>item.page)
.Select(group=>group.OrderBy(I=>I.priority))
You need each group to be ordered not the entire grouped enumerable.
try in second foreach
foreach (var item in group.OrderBy(i => i.priority))
if your only goal is to achieve sorting by two or more parameters use OrderBy(...).ThenBy(...)
I have listview with combox=true containg images. Each item is assigned a tag.
I can get tag of focussed item:
string name = this.lstview1.FocusedItem.Tag.ToString();
I can get index of checked item:
list = lstview1.CheckedIndices.Cast<int>().ToList();
How do I get the tag of the checked item?
You can use CheckedItems property instead of CheckedIndices:
var selectedTags = this.listView1.CheckedItems
.Cast<ListViewItem>()
.Select(x => x.Tag);
Anyway, also CheckedIndices can be used, e.g.:
var selectedTags = this.listView1.CheckedIndices
.Cast<int>()
.Select(i => this.listView1.Items[i].Tag);
EDIT:
Little explanation of LINQ Select():
The following code:
var selectedTags = this.listView1.CheckedItems
.Cast<ListViewItem>()
.Select(x => x.Tag);
foreach(var tag in selectedTags)
{
// do some operation using tag
}
is functionally equal to:
foreach(ListViewItem item in this.listView1.CheckedItems)
{
var tag = item.Tag;
// do some operation using tag
}
In this particular example is not so useful, nor shorter in term of code length, but, believe me, in many situations LINQ is really really helpful.
How about
var x = listView1.Items[listView1.CheckedIndices.Cast().ToList().First()].Tag;
?
I have a list of class items List<MyClass>
I have a seperate object that is of type MyClass
In my list I have an instance of this item but my where statement fails.
var home = Item.Find(23);
var item = allitems.Where(i => i == home);
item yields no results
allitems.Contains(home) also fails.
What am I doing wrong?
Are they definately tghe same items? If you have this situation;
var item1 = new Place(23);
var item2 = new Place(23);
then item1 != item2. If the items are identified by some property, you could try
allitems.Where(i => i.Id == home.Id)
Overriding Equals() in "Item" should also work.
var home = Item.Find(23);
var item = allitems.Where(i => i.Equals(home));