Что вам действительно нужно, так это кэшировать данные. Это занимает много времени, потому что вы должны опрашивать все каждый раз. Вот как я мог бы подойти к этой проблеме.
Создайте пару классов для структурированного хранения данных, которые вы извлекаете, а затем используйте словари для быстрого поиска проанализированных данных.
Вы может сделать что-то вроде этого:
Dictionary<Guid, CoSpace> CoSpaces = new Dictionary<Guid, CoSpace>();
OwnerDictionary Owners = new OwnerDictionary();
// Call to https://server/api/v1/coSpaces
// If persisting dictionaries instead of recreating them each time:
// List<Guid> currentCoSpaces = new List<Guid>();
foreach (<xml node enumeration>)
{
Guid coSpaceId = Guid.Parse("id from XML");
// If persisting dictionaries instead of recreating them each time:
// currentCoSpaces.Add(coSpaceId);
if (!CoSpaces.ContainsKey(coSpaceId))
{
// Populate the CoSpace with initial information
CoSpace coSpace = new CoSpace
{
Id = coSpaceId,
Name = "name from XML",
AutoGenerated = Convert.ToBoolean("autoGenerated from XML"),
Uri = Convert.ToInt32("uri from XML"),
CallId = Convert.ToInt32("callId from XML")
};
// Call to https://server/api/v1/coSpaces/<guid>
foreach (<2nd xml node enumeration>)
{
// Populate the CoSpace with the additional information
coSpace.NonMemberAccess = Convert.ToBoolean("nonMemberAccess from 2nd XML");
coSpace.Owner.Id = Guid.Parse("uri from 2nd XML");
coSpace.Owner.Jid = "autoGenerated from 2nd XML";
coSpace.Secret = "autoGenerated from 2nd XML";
// Track the CoSpace
CoSpaces.Add(coSpace.Id, coSpace);
// Track the CoSpace Owner
if (Owners.ContainsKey(coSpace.Owner))
Owners[coSpace.Owner].Add(coSpace);
else
Owners.Add(coSpace.Owner, new List<CoSpace>() { coSpace });
}
}
}
// If persisting dictionaries instead of recreating them each time:
//
// Call to https://server/api/v1/coSpaces/
//
// foreach (CoSpaceOwner owner in Owners.Keys)
// for (int index = Owners[owner].Count - 1; index >= 0; index--)
// if (!currentCoSpaces.Contains(Owners[owner][index].Id))
// Owners[owner].RemoveAt(index);
//
// Guid[] coSpaceIds = new Guid[CoSpaces.Keys.Count];
// CoSpaces.Keys.CopyTo(coSpaceIds, 0);
// foreach (Guid coSpaceId in coSpaceIds)
// if (!currentCoSpaces.Contains(coSpaceId))
// CoSpaces.Remove(coSpaceId);
// How to retrieve an all CoSpaces that belong to the same owner by searching by Jid
List<CoSpace> ownerCoSpaces = Owners[Owners.FindOwnerByJid("someone@work.tld")];
Классы для кода выше:
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
public class CoSpace : IEquatable<CoSpace>, IComparable<CoSpace>
{
public bool AutoGenerated { get; set; }
public int CallId { get; set; }
public Guid Id { get; set; }
public string Name { get; set; }
public bool NonMemberAccess { get; set; }
public CoSpaceOwner Owner { get; set; }
public string Secret { get; set; }
public int Uri { get; set; }
public CoSpace()
{
Owner = new CoSpaceOwner();
}
public static bool operator !=(CoSpace left, CoSpace right)
{
return !(left == right);
}
public static bool operator <(CoSpace left, CoSpace right)
{
return left is null ? right is object : left.CompareTo(right) < 0;
}
public static bool operator <=(CoSpace left, CoSpace right)
{
return left is null || left.CompareTo(right) <= 0;
}
public static bool operator ==(CoSpace left, CoSpace right)
{
if (left is null)
{
return right is null;
}
return left.Equals(right);
}
public static bool operator >(CoSpace left, CoSpace right)
{
return left is object && left.CompareTo(right) > 0;
}
public static bool operator >=(CoSpace left, CoSpace right)
{
return left is null ? right is null : left.CompareTo(right) >= 0;
}
public int CompareTo(CoSpace other)
{
return Id.CompareTo(other.Id);
}
public bool Equals(CoSpace other)
{
return Id.Equals(other.Id);
}
public override bool Equals(object other)
{
if (other.GetType() == typeof(CoSpace))
return Equals((CoSpace)other);
if (ReferenceEquals(this, other))
{
return true;
}
if (other is null)
{
return false;
}
return false;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override string ToString()
{
return Name;
}
}
public class CoSpaceOwner : IEquatable<CoSpaceOwner>, IComparable<CoSpaceOwner>
{
public Guid Id { get; set; }
public string Jid { get; set; }
public static bool operator !=(CoSpaceOwner left, CoSpaceOwner right)
{
return !(left == right);
}
public static bool operator <(CoSpaceOwner left, CoSpaceOwner right)
{
return left is null ? right is object : left.CompareTo(right) < 0;
}
public static bool operator <=(CoSpaceOwner left, CoSpaceOwner right)
{
return left is null || left.CompareTo(right) <= 0;
}
public static bool operator ==(CoSpaceOwner left, CoSpaceOwner right)
{
if (left is null)
return right is null;
return left.Equals(right);
}
public static bool operator >(CoSpaceOwner left, CoSpaceOwner right)
{
return left is object && left.CompareTo(right) > 0;
}
public static bool operator >=(CoSpaceOwner left, CoSpaceOwner right)
{
return left is null ? right is null : left.CompareTo(right) >= 0;
}
public int CompareTo(CoSpaceOwner other)
{
return Id.CompareTo(other.Id);
}
public bool Equals(CoSpaceOwner other)
{
return Id.Equals(other.Id) & Jid.Equals(other.Jid);
}
public override bool Equals(object other)
{
if (other.GetType() == typeof(CoSpaceOwner))
return Equals((CoSpaceOwner)other);
if (ReferenceEquals(this, other))
return true;
if (other is null)
return false;
return false;
}
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override string ToString()
{
return Jid;
}
}
[Serializable]
public class OwnerDictionary : Dictionary<CoSpaceOwner, List<CoSpace>>
{
public OwnerDictionary()
: base()
{
}
protected OwnerDictionary(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
public CoSpaceOwner FindOwnerById(Guid id)
{
foreach (CoSpaceOwner owner in this.Keys)
if (owner.Id.Equals(id))
return owner;
return null;
}
public CoSpaceOwner FindOwnerByJid(string jid)
{
foreach (CoSpaceOwner owner in this.Keys)
if (owner.Jid.Equals(jid))
return owner;
return null;
}
}