我正在尝试使用文档提供的稍微不同的结构将对象添加到我的Algolia数据库中,这样我就不必以字符串格式输出Json对象,但是我遇到了一个错误,指出< / p>
无法隐式转换匿名类型 System.Collections.Generic.List
我看到objs
变量中所有键/值的红色错误消息。
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);
以下是对文档的引用:https://www.algolia.com/doc/api-reference/api-methods/add-objects/
编辑替代方法但是,我的LicenseFL格式已关闭
List<JObject> objs = new List<JObject>();
objs.Add(JObject.Parse(@"{""ApprovalFL"":false, ""FreeFL"":" + album.FreeFL + ",""LicenseFL"":" +album.LicenseFL+ "}"));
songIndexHelper.AddObjects(objs);
答案 0 :(得分:0)
The Algolia docs are unfortunately focused on the use of @model Proovitoo_v4.Models.DatePickerModel
@{
ViewBag.Title = "Index";
}
@using (Html.BeginForm())
{
<label>Start date</label><br />
@Html.TextBoxFor(model => model.StartDate, new { @class = "datepicker" })
<br />
<label>End date</label><br />
@Html.TextBoxFor(model => model.EndDate, new { @class = "datepicker" })
}
<br />
<br />
<input id="btnSubmit" type="button" value="Show electricity consumption"/>
<br />
@section scripts{
<script src="~/Scripts/jquery-ui-1.12.1.min.js"></script>
<script>
$(function algus() {
$(".datepicker").datepicker({
dateFormat: "dd.mm.yy",
changeMonth: true,
changeYear: true,
showWeek: true,
yearRange: "2015:2018",
minDate: new Date(2010, 0, 1),
maxDate: new Date(2018, 0, 1),
showOn: "both",
buttonText: "Select"
});
});
$(function () {
$("#startdate").datepicker({ dateformat: "dd.mm.yy" });
$("#enddate").datepicker({ dateformat: "dd.mm.yy" });
$("#btnSubmit").click(function (e) {
e.preventDefault();
var startdate = $("#StartDate").val();
console.log(startdate);
var enddate = $("#EndDate").val();
console.log(enddate);
var datefilter = { StartDate: startdate, EndDate: enddate };
console.log(datefilter);
console.log('@Url.Action("FilterByDate", "DatePicker")');
$.ajax({
url: "@Url.Action("FilterByDate", "DatePicker")",
type: "POST",
data: JSON.stringify(datefilter),
contentType: 'application/json; charset=utf-8'
})
.done(function (datepicker) {
$("#res").html(tempConsumtion);
});
});
});
</script>
}
(and JSON strings) which makes it reasonably easy to make mistakes (e.g. invalid JSON).
This is an approach you might like to consider:
JObject
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 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);
).