public static OdataObjectCollection <T, TId> AsOdata <T, TId>(this IEnumerable <T> ts, Uri uri = null, params string[] properties) { var leftPart = uri?.GetLeftPart(UriPartial.Path); var uriKind = uri != null && uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative; var entities = ts.Select(t => t.AsOdata <T, TId>(leftPart, true, uriKind, properties)).ToList(); var collection = new OdataObjectCollection <T, TId>(); collection.AddRange(entities); return(collection); }
/// <summary> /// A method to easily get the related entity. /// </summary> /// <typeparam name="TRelatedEntity"></typeparam> /// <typeparam name="TRelatedEntityId"></typeparam> /// <param name="odataObj"></param> /// <returns></returns> public static OdataObjectCollection <TRelatedEntity, TRelatedEntityId> GetRelatedEntityCollection <TRelatedEntity, TRelatedEntityId>(this IRelatedEntityCollection odataObj, string entityAlias = null) where TRelatedEntityId : IComparable <TRelatedEntityId>, IComparable, IEquatable <TRelatedEntityId> { if (odataObj == null || odataObj.RelatedEntityCollection == null || !odataObj.RelatedEntityCollection.Any()) { return(null); } var relatedEntity = entityAlias ?? typeof(TRelatedEntity).Name; var relatedEntityCollection = odataObj.RelatedEntityCollection.FirstOrDefault(re => re.RelatedEntity == relatedEntity); if (relatedEntityCollection == null) { return(null); } var collection = new OdataObjectCollection <TRelatedEntity, TRelatedEntityId>(); foreach (var item in relatedEntityCollection) { var obj = JsonConvert.DeserializeObject <TRelatedEntity>(item.Object.ToString()); var odataItem = item.ToOdataObject <TRelatedEntity, TRelatedEntityId>(); collection.Add(odataItem); } return(collection); }