C # filter elements in a list according to several criteria

Firstly, my situation is here ...

  • My SomeObject has a property string Statusthat interests me for this scenario.
  • The Status property can contain the values ​​"Open", "Closed", "Finish".
  • I have a method called FilterObjectsthat returnsList<SomeObject>
  • The method takes an argument in the same way as its return type, List<SomeObject>
  • The method is supposed to be filtered in accordance with the following cases described below and return a list of objects.
  • List<SomeObject> I send as an argument to my method, it will be guaranteed to be in order (through their identifier and type).

Cases (all related to the property string Statusthat I mentioned):

  • If any element in the list contains Status = "Finished";, then eliminate all other elements that were in the original list, and return only the object with the status "Finished".
  • If any element does NOT contain Status = Finished, but contains "CLOSED", I need to check if there is any other element that has the value "Open" after that, "CLOSED" is "one. You can think of it as a" task, which can be closed, but can be reopened, but once it is finished, it cannot be opened. "

    "" "" , . "" , - , .

MS Paint.

enter image description here

, :

private List<SomeObject> FilterObjects(List<SomeObject> objectList)
{
    var objects = objectList;
    var returnList = new List<SomeObject>();

    foreach (var obj in objects)
    {
        if (obj.Status == "Finished")
        {
            returnList.Add(obj);
            return returnList;
        }
    }

    return new List<SomeObject>();
}

, ? , , , , FINISHED. LINQ-?

, , , , .

.

+4
6

- :

private List<SomeObject> FilterObjects(List<SomeObject> objectList)
{
    SomeObject finished = objectList.FirstOrDefault(o => o.Status.Equals("Finished"));
    if (finished != null) { return new List<SomeObject> { finished }; }

    List<SomeObject> closed = objectList.SkipWhile(o => !o.Status.Equals("Closed")).ToList();
    if (closed.Count == 1) { return closed; }
    if (closed.Count > 1) { return closed.Skip(1).ToList(); }

    // if you need a new list object than return new List<SomeObject>(objectList);
    return objectList;
}
+4

Linq , , . - :

    private List<SomeObject> FilterObjects(List<SomeObject> objectList)
    {
        int lastClosed = -1;
        for (int i = 0; i < objectList.Count; i++)
        {
            if (objectList[i].Status == "Closed")
                lastClosed = i;
            else if (objectList[i].Status == "Finished")
                return new List<SomeObject>() { objectList[i] };
        }

        if (lastClosed > -1)
            if (lastClosed == objectList.Count - 1)
                return new List<SomeObject>() { objectList[lastClosed] };
            else 
                return objectList.Skip(lastClosed + 1).ToList();
        else
            return objectList;
    }

EDIT: , , objectList .

+3

LINQ , / .

Closed , . , Closed , .

static List<SomeObject> FilterObjects(List<SomeObject> objectList)
{
    int pos = 0;
    bool closed = false;
    for (int i = 0; i < objectList.Count; i++)
    {
        var item = objectList[i];

        if (item.Status == "Finished")
            return new List<SomeObject> { item };

        if (item.Status == (closed ? "Opened" : "Closed"))
        {
            pos = i;
            closed = !closed;
        }
    }
    return objectList.GetRange(pos, closed ? 1 : objectList.Count - pos);
}
+2

:

public static IEnumerable<SomeObject> convert(this IEnumerable<SomeObject> input)
{
    var finished = input.FirstOrDefault(x => x.Status == "Finished");
    if (finished != null)
    {
        return new List<SomeObject> {finished};
    }
   return input.Aggregate(new List<SomeObject>(), (a, b) =>
   {
       if (!a.Any())
       {
          a.Add(b);
       }
       else if (b.Status == "Open")
       {
          if (a.Last().Status == "Closed")
          {
            a.Remove(a.Last());
          }
          a.Add(b);
       }
       else if (b.Status == "Closed")
       {
          a = new List<SomeObject> {b};
       }
       return a;
   });
}
+1

. , .

public List<SomeCls> GetResult(List<SomeCls> lstData)
    {
        List<SomeCls> lstResult;

        if(lstData.Any(x=>x.Status=="Finished"))
        {
            lstResult = lstData.Where(x=>x.Status=="Finished").ToList();
        }

        else if(lstData.Any(x=>x.Status=="Closed"))
        {
            // Here assuming that there is only one Closed in whole list
            int index = lstData.FindIndex(0,lstData.Count(),x=>x.Status=="Closed");
            lstResult = lstData.GetRange(index,lstData.Count()-index);

            if(lstResult.Count()!=1) // check if it contains Open.
            {
                lstResult = lstResult.Where(x=>x.Status=="Open").ToList();
            }
        }
        else // Only Open
        {
            lstResult = lstData;
        }
        return lstResult;
    }
0

- :

    private List<SomeObject> FilterObjects(List<SomeObject> objectList)
    {
        if (objectList.Where(x => x.Status == "Finished").Any())
        {
            return objectList.Where(x => x.Status == "Finished").ToList();
        }

        else if (objectList.Where(x => x.Status == "Closed").Any())
        {
            if (objectList.FindIndex(x => x.Status == "Closed") == objectList.Count() - 1)
            {
                return objectList.Where(x => x.Status == "Closed").ToList();
            }
            else
            {
                return objectList.GetRange(objectList.FindIndex(x => x.Status == "Closed") + 1, objectList.Count() - (objectList.FindIndex(x => x.Status == "Closed") + 1));
            }
        }

        return objectList;
     }
0

Source: https://habr.com/ru/post/1667952/


All Articles