An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Conditional selects on expanded navigation not supported
Good day,
I'm attempting to retrieve data from an OData service, using the Microsoft.OData.Client nuget package.
The end result that I'm looking for is to execute the following /api/data/v9.2/contacts?$top=5&$expand=masterContact($select=name)&$select=contactid.
I want to use linq and poco classes rather than an httpclient to do this and deserializing the results.
The code I've implemented is as follows:
var testContacts = _context.Contacts
.Select(x => new { Id = x.Id, Details = x.Detail!.Name })
.Take(5)
.ToList();
This results in a null reference exception as Detail could be null. In addition, I cannot check for null like the below either
// not supported
var testContacts = _context.Contacts
.Select(x => new { Id = x.Id, Details = x.Detail?.Name })
.Take(5)
.ToList();
// not supported
var testContacts = _context.Contacts
.Select(x => new { Id = x.Id, Details = x.Detail != null ? x.Detail.Name : string.Empty })
.Take(5)
.ToList();
Using Simple.OData.V4.Client nuget I can achieve the desired result:
var test = await _client
.For<Contact>()
.Top(5)
.Expand(x => x.Detail!)
.Select(x => new { Id = x.Id, test = x.Detail!.Name })
.FindEntriesAsync(cancellationToken);
How can I use MS's nuget package to achieve this?