Weiß Nicht

Weiß Nicht

  • NA
  • 20
  • 776

How do I "select new" in a OData-V4 Linq Query?

Aug 24 2021 1:18 PM

I used the following code successfully to get data from NAV/BC (ERP).

myuri = new Uri(Properties.Settings.Default.ODataCompanyURL);
nav = new NAV.NAV(myuri);
myCache.Add(myuri, "Basic", new NetworkCredential(txtBoxUser.Text, txtBoxPW.Text));
nav.Credentials = myCache;
var log10 = from c in nav.Change_Log_Entrieswhere c.Table_No == Properties.Settings.Default.TableNoorderby c.Date_and_Time descendingselect new { Record = c.Primary_Key_Field_1_Value, User = c.User_ID };

This worked well for OData Webservice V3. I updated the connected service, the metadata now refers to Odata V4 URL.

Once I run the above Code, I receive the following errors:

Inner Exception 1:

DataServiceClientException: {"error":{"code":"Unknown","message":"The entity instance value of type 'NAV.Change_Log_Entries' doesn't have a value for property 'Entry_No'. To compute an entity's metadata, its key and concurrency-token property values must be provided. CorrelationId: 4ec280ea-5a5d-4a41-96eb-91849aa1adaf."}}

Inner Exception 2:

ODataErrorException: An error was read from the payload. See the 'Error' property for more details.

After experimenting for a while, I found out that the select part of the query seems to be the problem.

If I change the query to this (selecting all):

var log10 = from c in nav.Change_Log_Entrieswhere c.Table_No == Properties.Settings.Default.TableNoorderby c.Date_and_Time descendingselect c;

I'm getting data again, but I obviously cannot preselect my desired columns...

Many thanks in advance!!