Before the changes introduced by $select and $expand, defining custom odata links was reasonably straightforward using EntityInstanceContext.EntityInstance as in the following snippet.
```
eic =>
{
DerivedEntity entity = eic.EntityInstance as DerivedEntity;
if (entity == null || entity.Id % 2 == 1)
{
return null;
}
else
{
IList<ODataPathSegment> segments = new List<ODataPathSegment>();
segments.Add(new EntitySetPathSegment(eic.EntitySet));
segments.Add(new KeyValuePathSegment(entity.Id.ToString()));
segments.Add(new CastPathSegment(entity.GetType().FullName));
segments.Add(new ActionPathSegment("TransientActionDerivedType"));
string link = eic.Url.ODataLink("Actions", eic.Request.GetODataPathHandler(), segments);
return new Uri(link);
}
};
```
With the changes introduced in EntityInstanceContext by $select and $expand, the user is forced to access the EdmObject property, which is basically a Dictionary, instead of the EntityInstance property that now is null. The following is a sample of the new code that the user needs to write.
```
eic =>
{
IEdmEntityType derivedType = eic.EdmModel.FindType(typeof(DerivedEntity).FullName) as IEdmEntityType;
object id;
eic.EdmObject.TryGetValue("Id", out id);
if (!eic.EntityType.IsOrInheritsFrom(derivedType) || (int)id % 2 == 1)
{
return null;
}
else
{
IList<ODataPathSegment> segments = new List<ODataPathSegment>();
segments.Add(new EntitySetPathSegment(eic.EntitySet));
segments.Add(new KeyValuePathSegment(id.ToString()));
segments.Add(new CastPathSegment(derivedType.FullName()));
segments.Add(new ActionPathSegment("TransientActionDerivedType"));
string link = eic.Url.ODataLink("Actions", eic.Request.GetODataPathHandler(), segments);
return new Uri(link);
}
};
```
Place special attention at:
__object id;
eic.EdmObject.TryGetValue("Id", out id);__
EdmObject could provide a safer, typed way to access the elements in it as $select doesn't perform any kind of renaming. For example:
eic.EdmObject.GetPropertyAs<EntityType>(x => x.Property);
This way, the custom link generation would be much more similar to what it used to be.
```
eic =>
{
DerivedEntity entity = eic.EntityInstance as DerivedEntity;
if (entity == null || entity.Id % 2 == 1)
{
return null;
}
else
{
IList<ODataPathSegment> segments = new List<ODataPathSegment>();
segments.Add(new EntitySetPathSegment(eic.EntitySet));
segments.Add(new KeyValuePathSegment(entity.Id.ToString()));
segments.Add(new CastPathSegment(entity.GetType().FullName));
segments.Add(new ActionPathSegment("TransientActionDerivedType"));
string link = eic.Url.ODataLink("Actions", eic.Request.GetODataPathHandler(), segments);
return new Uri(link);
}
};
```
With the changes introduced in EntityInstanceContext by $select and $expand, the user is forced to access the EdmObject property, which is basically a Dictionary, instead of the EntityInstance property that now is null. The following is a sample of the new code that the user needs to write.
```
eic =>
{
IEdmEntityType derivedType = eic.EdmModel.FindType(typeof(DerivedEntity).FullName) as IEdmEntityType;
object id;
eic.EdmObject.TryGetValue("Id", out id);
if (!eic.EntityType.IsOrInheritsFrom(derivedType) || (int)id % 2 == 1)
{
return null;
}
else
{
IList<ODataPathSegment> segments = new List<ODataPathSegment>();
segments.Add(new EntitySetPathSegment(eic.EntitySet));
segments.Add(new KeyValuePathSegment(id.ToString()));
segments.Add(new CastPathSegment(derivedType.FullName()));
segments.Add(new ActionPathSegment("TransientActionDerivedType"));
string link = eic.Url.ODataLink("Actions", eic.Request.GetODataPathHandler(), segments);
return new Uri(link);
}
};
```
Place special attention at:
__object id;
eic.EdmObject.TryGetValue("Id", out id);__
EdmObject could provide a safer, typed way to access the elements in it as $select doesn't perform any kind of renaming. For example:
eic.EdmObject.GetPropertyAs<EntityType>(x => x.Property);
This way, the custom link generation would be much more similar to what it used to be.