Share via

Conditional selects on expanded navigation not supported

Ricky Schuleman 35 Reputation points
2023-03-29T15:14:44.07+00:00

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?

Developer technologies | C#
Developer technologies | C#

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.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.