Performance issues with repeatable loops as control part

Posted by djerry on Stack Overflow See other posts from Stack Overflow or by djerry
Published on 2010-04-28T12:59:06Z Indexed on 2010/04/28 13:03 UTC
Read the original article Hit count: 254

Filed under:
|
|

Hey guys,

In my application, i need to show made calls to the user. The user can arrange some filters, according to what they want to see. The problem is that i find it quite hard to filter the calls without losing performance. This is what i am using now :

private void ProcessFilterChoice()
    {
        _filteredCalls = ServiceConnector.ServiceConnector.SingletonServiceConnector.Proxy.GetAllCalls().ToList();
        if (cboOutgoingIncoming.SelectedIndex > -1)
            GetFilterPartOutgoingIncoming();
        if (cboInternExtern.SelectedIndex > -1)
            GetFilterPartInternExtern();
        if (cboDateFilter.SelectedIndex > -1)
            GetFilteredCallsByDate();
        wbPdf.Source = null;
        btnPrint.Content = "Pdf preview";
    }
    private void GetFilterPartOutgoingIncoming()
    {
        if (cboOutgoingIncoming.SelectedItem.ToString().Equals("Outgoing"))
            for (int i = _filteredCalls.Count - 1; i > -1; i--)
            {
                if (_filteredCalls[i].Caller.E164.Length > 4 || _filteredCalls[i].Caller.E164.Equals("0"))
                    _filteredCalls.RemoveAt(i);
            }
        else if (cboOutgoingIncoming.SelectedItem.ToString().Equals("Incoming"))
            for (int i = _filteredCalls.Count - 1; i > -1; i--)
            {
                if (_filteredCalls[i].Called.E164.Length > 4 || _filteredCalls[i].Called.E164.Equals("0"))
                    _filteredCalls.RemoveAt(i);
            }
    }
    private void GetFilterPartInternExtern()
    {
        if (cboInternExtern.SelectedItem.ToString().Equals("Intern"))
            for (int i = _filteredCalls.Count - 1; i > -1; i--)
            {
                if (_filteredCalls[i].Called.E164.Length > 4 || _filteredCalls[i].Caller.E164.Length > 4 || _filteredCalls[i].Caller.E164.Equals("0"))
                    _filteredCalls.RemoveAt(i);
            }
        else if (cboInternExtern.SelectedItem.ToString().Equals("Extern"))
            for (int i = _filteredCalls.Count - 1; i > -1; i--)
            {
                if ((_filteredCalls[i].Called.E164.Length < 5 && _filteredCalls[i].Caller.E164.Length < 5) || _filteredCalls[i].Called.E164.Equals("0"))
                    _filteredCalls.RemoveAt(i);
            }
    }
    private void GetFilteredCallsByDate()
    {
        DateTime period = DateTime.Now;
        switch (cboDateFilter.SelectedItem.ToString())
        {
            case "Today":
                period = DateTime.Today;
                break;
            case "Last week":
                period = DateTime.Today.Subtract(new TimeSpan(7, 0, 0, 0));
                break;
            case "Last month":
                period = DateTime.Today.AddMonths(-1);
                break;
            case "Last year":
                period = DateTime.Today.AddYears(-1);
                break;
            default:
                return;
        }
        for (int i = _filteredCalls.Count - 1; i > -1; i--)
        {
            if (_filteredCalls[i].Start < period)
                _filteredCalls.RemoveAt(i);
        }
    }

_filtered calls is a list of "calls". Calls is a class that looks like this :

[DataContract]
public class Call
{
    private User caller, called;
    private DateTime start, end;
    private string conferenceId;
    private int id;
    private bool isNew = false;

    [DataMember]
    public bool IsNew
    {
        get { return isNew; }
        set { isNew = value; }
    }
    [DataMember]
    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    [DataMember]
    public string ConferenceId
    {
        get { return conferenceId; }
        set { conferenceId = value; }
    }
    [DataMember]
    public DateTime End
    {
        get { return end; }
        set { end = value; }
    }
    [DataMember]
    public DateTime Start
    {
        get { return start; }
        set { start = value; }
    }
    [DataMember]
    public User Called
    {
        get { return called; }
        set { called = value; }
    }
    [DataMember]
    public User Caller
    {
        get { return caller; }
        set { caller = value; }
    }

Can anyone direct me to a better solution or make some suggestions.

© Stack Overflow or respective owner

Related posts about c#

Related posts about Performance