如何在 Observable Collection 中搜索项目并获取其索引

发布于 2025-01-07 07:40:03 字数 768 浏览 0 评论 0原文

public struct PLU
{ 
    public int ID { get; set; } 
    public string name { get; set; } 
    public double price { get; set; } 
    public int quantity {get;set;}
}

public static ObservableCollection<PLU> PLUList = new ObservableCollection<PLU>();

我有上面的 ObservableCollection 。现在我想在 PLUList 中搜索 ID 并获取其索引,如下所示:

int index = PLUList.indexOf();
if (index > -1)
{
    // Do something here
}
else
{
    // Do sth else here..
}

有什么快速修复方法?

编辑:

假设一些项目已添加到 PLUList 中,并且我想添加另一个新项目。但在添加之前,我想检查 ID 是否已存在于列表中。如果确实如此,那么我想为数量添加+1。

public struct PLU
{ 
    public int ID { get; set; } 
    public string name { get; set; } 
    public double price { get; set; } 
    public int quantity {get;set;}
}

public static ObservableCollection<PLU> PLUList = new ObservableCollection<PLU>();

I have the ObservableCollection as above. Now I want to search the ID in the PLUList and get its index like this:

int index = PLUList.indexOf();
if (index > -1)
{
    // Do something here
}
else
{
    // Do sth else here..
}

What's the quick fix?

EDIT:

Let's assume that some items were added to PLUList and I want to add another new item. But before adding I want to check if the ID already exists in the list. If it does then I would like to add +1 to the quantity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

度的依靠╰つ 2025-01-14 07:40:03

使用 LINQ :-)

var q =  PLUList.Where(X => X.ID == 13).FirstOrDefault();
if(q != null) 
{
   // do stuff
}
else 
{
   // do other stuff
}

如果你想将其保留为结构体,请使用它:

var q =  PLUList.IndexOf( PLUList.Where(X => X.ID == 13).FirstOrDefault() );
if(q > -1) 
{
   // do stuff
}
else 
{
   // do other stuff
}

Use LINQ :-)

var q =  PLUList.Where(X => X.ID == 13).FirstOrDefault();
if(q != null) 
{
   // do stuff
}
else 
{
   // do other stuff
}

Use this, if you want to keep it a struct:

var q =  PLUList.IndexOf( PLUList.Where(X => X.ID == 13).FirstOrDefault() );
if(q > -1) 
{
   // do stuff
}
else 
{
   // do other stuff
}
梦回梦里 2025-01-14 07:40:03

如果您想从列表中检索项目,只需使用 LINQ:

PLU item = PLUList.Where(z => z.ID == 12).FirstOrDefault();

但这将返回项目本身,而不是其索引。为什么要索引?

另外,如果可能的话,您应该使用 class 而不是 struct。然后,您可以针对 null 测试 item 以查看是否在集合中找到了 ID

if (item != null)
{
    // Then the item was found
}
else
{
    // No item found !
}

If you want to retrieve the item from your list, just use LINQ:

PLU item = PLUList.Where(z => z.ID == 12).FirstOrDefault();

But this will return the item itself, not its index. Why do you want the index?

Also, you should use class instead of struct if possible. Then you could test item against null to see if the ID was found in the collection.

if (item != null)
{
    // Then the item was found
}
else
{
    // No item found !
}
有深☉意 2025-01-14 07:40:03

虽然这篇文章很旧并且已经得到回答,但它仍然对其他人有帮助,所以我在这里是我的答案。

您可以创建类似于 List.FindIndex(...) 方法的扩展方法:

public static class ObservableCollectionExtensions
{
    public static int FindIndex<T>(this ObservableCollection<T> ts, Predicate<T> match)
    {
        return ts.FindIndex(0, ts.Count, match);
    }

    public static int FindIndex<T>(this ObservableCollection<T> ts, int startIndex, Predicate<T> match)
    {
        return ts.FindIndex(startIndex, ts.Count, match);
    }

    public static int FindIndex<T>(this ObservableCollection<T> ts, int startIndex, int count, Predicate<T> match)
    {
        if (startIndex < 0) startIndex = 0;
        if (count > ts.Count) count = ts.Count;

        for (int i = startIndex; i < count; i++)
        {
            if (match(ts[i])) return i;
        }

        return -1;
    }
}

用法:

int index = PLUList.FindIndex(x => x.ID == 13);
if (index > -1)
{
    // Do something here...
}
else
{
    // Do something else here...
}

Although this post is old and already answered, it can still be helpful to others so I here is my answer.

You can create extension methods similar to List<T>.FindIndex(...) methods:

public static class ObservableCollectionExtensions
{
    public static int FindIndex<T>(this ObservableCollection<T> ts, Predicate<T> match)
    {
        return ts.FindIndex(0, ts.Count, match);
    }

    public static int FindIndex<T>(this ObservableCollection<T> ts, int startIndex, Predicate<T> match)
    {
        return ts.FindIndex(startIndex, ts.Count, match);
    }

    public static int FindIndex<T>(this ObservableCollection<T> ts, int startIndex, int count, Predicate<T> match)
    {
        if (startIndex < 0) startIndex = 0;
        if (count > ts.Count) count = ts.Count;

        for (int i = startIndex; i < count; i++)
        {
            if (match(ts[i])) return i;
        }

        return -1;
    }
}

Usage:

int index = PLUList.FindIndex(x => x.ID == 13);
if (index > -1)
{
    // Do something here...
}
else
{
    // Do something else here...
}
余生再见 2025-01-14 07:40:03

这是一个快速修复。

int findID = 3;
int foundID=  -1;
for (int i = 0; i< PLUList.Count; i++)
{
  if (PLUList[i].ID == findID)
  {
    foundID = i;
    break;
  }
}

// Your code.
if (foundID > -1) {
// Do something here
...

Here is a quick fix.

int findID = 3;
int foundID=  -1;
for (int i = 0; i< PLUList.Count; i++)
{
  if (PLUList[i].ID == findID)
  {
    foundID = i;
    break;
  }
}

// Your code.
if (foundID > -1) {
// Do something here
...
吲‖鸣 2025-01-14 07:40:03

这只是一个普通的集合。您可以迭代它,检查 ID 并返回对象的索引。

int index = -1;

for(int i=0;i<PLUList.Count;i++) {
 PLU plu = PLUList[i];
 if (plu.ID == yourId) {
   index = i;
   break;
 }
}

if (index > -1) {
// Do something here
}
else {
// Do sth else here..
}

林克版本:

private void getIndexForID(PLUListint idToFind,ObservableCollection<PLU> PLUList) {
   PLU target = PLUList.Where( z => z.ID == yourID ).FirstOrDefault();
   return target == null ? -1 : PLUList.IndexOf ( target );
}

It's just a normal collection. You can just iterate over it, check the ID and return the index of the object.

int index = -1;

for(int i=0;i<PLUList.Count;i++) {
 PLU plu = PLUList[i];
 if (plu.ID == yourId) {
   index = i;
   break;
 }
}

if (index > -1) {
// Do something here
}
else {
// Do sth else here..
}

LINQ VERSION:

private void getIndexForID(PLUListint idToFind,ObservableCollection<PLU> PLUList) {
   PLU target = PLUList.Where( z => z.ID == yourID ).FirstOrDefault();
   return target == null ? -1 : PLUList.IndexOf ( target );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文