-1

I am trying to add an object to my Algolia database using a slightly different structure provided by the documentation so that I don't have to type out the Json object in string format, however I ran into an error stating

Cannot implicitly convert anonymous type to System.Collections.Generic.List

I see the red error message for all of the key/values in the objs variable.

var songIndexHelper = HttpContext.Application.Get("SongIndexHelper") as IndexHelper<SongAlgoliaModel>;

List<JObject> objs = new List<JObject>();

objs = new
{
    ApprovalFL = false,
    FreeFL = album.FreeFL,
    LicenseFL = album.LicenseFL,
    AccountInfoID = album.AccountInfoID,
    AlbumID = album.AlbumID,
    SongID = song.SongID,
    BPM = song.BPM,
    AccountImageURL = album.AccountInfo.ImageURL,
    AccountType = "Artist",
    AlbumName = album.AlbumName,
    Artist = artist,
    FeaturedArtist = songArtistsList,
    ImageURL = album.ImageURL,
    iTunesURL = album.iTunesURL,
    LabelName = album.LabelName,
    Title = album.AlbumName,
    UserID = album.AccountInfo.UserID,
    UploadDate = song.UploadDate,
    Duration = song.Duration,
    objectID = song.SongID
};

songIndexHelper.AddObjects(objs);

Here's the reference to documentation: https://www.algolia.com/doc/api-reference/api-methods/add-objects/

Edit alternative method however, my formatting of LicenseFL is off

List<JObject> objs = new List<JObject>();
objs.Add(JObject.Parse(@"{""ApprovalFL"":false, ""FreeFL"":" + album.FreeFL + ",""LicenseFL"":" +album.LicenseFL+ "}"));

songIndexHelper.AddObjects(objs);
3
  • 1
    A) objs is a List<JObject> B) You are reassigning it an instance of an anonymous type -- which is not a list C) Even if it were a list, it would not be a list of JObject 1) You'd need to convert your anonymous type into a JObject (JObject.FromObject(...) might suffice) 2) You'd need to add that to the list, rather than re-assigning the list Commented Dec 18, 2017 at 0:13
  • @KirkWoll thanks for the help so far, I was referencing a suggestion from a question I asked earlier today (his second method), found here: stackoverflow.com/a/47859205/6480913 in an attempt to get this to work properly in a different situation Commented Dec 18, 2017 at 0:20
  • @mjwills to be honest, might be more straightforward from a string json standpoint. But yet again my formatting is off on the json LicenseFL. States it is expecting a comma after the String LicenseFL key (updated original question) Commented Dec 18, 2017 at 2:45

1 Answer 1

0

The Algolia docs are unfortunately focused on the use of JObject (and JSON strings) which makes it reasonably easy to make mistakes (e.g. invalid JSON).

This is an approach you might like to consider:

var anon = new
{
    ApprovalFL = true,
    // Any other properties here
    objectID = song.SongID
};

var obj = JObject.FromObject(anon);
var objs = new List<JObject> { obj };
songIndexHelper.AddObjects(objs);

Now you get some level of safety due to the anon variable (e.g. don't have to worry about invalid JSON strings) but also easy interaction with the Algolia API (which is documented in terms of JObject).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.