creating an order using square connect api - square-connect

I have been trying to create an order with following values but I am getting the Quantity null error even though the value for Quantity has been set clearly. Here is the code snippet.
var item = new CreateOrderRequestLineItem()
{
//Name = "Yaar Book",
//Note = "New Book",
CatalogObjectId = "STWMISUMLIIIXU6MVWNFX6FQ",
Quantity = "1",
BasePriceMoney = new Money() { Amount = 20, Currency = Money.CurrencyEnum.NPR }
};
And got the following error:
System.IO.InvalidDataException: 'Quantity is a required property for
CreateOrderRequestLineItem and cannot be null'
First I tried just by putting the name, but got the error, then I used the catalog id instead of just a name and still getting the same error.
I am using v2 of Connect API.
Thanks.

This is how CreateOrderRequestLineItem should have been instantiated:
var item = new CreateOrderRequestLineItem(
CatalogObjectId: "STWMISUMLIIIXU6MVWNFX6FQ",
Quantity: "1",
BasePriceMoney: new Money() { Amount = 20, Currency = Money.CurrencyEnum.NPR });

Related

Elastic Search Update API by script in NEST 6.5.4 for .NET

I am using Nest 6.5.4. I am not able to perform Update with script on a particular document in an index.
I have tried many ways but I am getting a syntax error.
My query is as follows.
var clientProvider = new ElasticClientProvider();
var projectModel = new ProjectModel();
var res = clientProvider.Client.Update<ProjectModel>(projectModel, i => i
.Index("attachment_index")
.Type("attachments")
.Id(projectId)
.Script(script=>script.Source("ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"))
);
It is throwing an error "Update Descriptor does not have a definition for Id"
The same query is working when tried in Kibana
POST attachment_index/attachments/1/_update
{
"script": {
"source":"ctx._source.fileInfo.fileViewCount += 1"
}
}
I dont know where I am getting error.
client.UpdateAsync<ProjectModel, object>(
new DocumentPath<ProjectModel>(id),
u => u
.Index(ConfigurationManager.AppSettings.Get("indexname"))
.Type(ConfigurationManager.AppSettings.Get("indextype"))
.Doc(ProjectModel)
.DocAsUpsert()
.Refresh(Elasticsearch.Net.Refresh.True));
This will work and let me know if you are still facing any issues.
There is no .Id() method on UpdateDescriptor<T, TPartial> because an id is a required parameter for an Update API call, so this constraint is enforced through the constructors.
The first parameter to .Update<T>(...) is a DocumentPath<T> from which an index, type and id can be derived for the update API call. If the ProjectModel CLR POCO has an Id property with a value, this will be used for Id of the call. For example
public class ProjectModel
{
public int Id { get; set; }
}
var client = new ElasticClient();
var projectModel = new ProjectModel { Id = 1 };
var updateResponse = client.Update<ProjectModel>(projectModel, i => i
.Index("attachment_index")
.Type("attachments")
.Script(script => script
.Source("ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"))
);
which results in
POST http://localhost:9200/attachment_index/attachments/1/_update
{
"script": {
"source": "ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"
}
}
If you want to explicitly specify the Id, you can pass the value for DocumentPath<T>
var updateResponse = client.Update<ProjectModel>(1, i => i
.Index("attachment_index")
.Type("attachments")
.Script(script => script
.Source("ctx._source.fileInfo.fileViewCount= ctx._source.fileInfo.fileViewCount + 1"))
);

linq count error: DbExpressionBinding requires an input expression with a collection ResultType. Parameter name: input

I'm trying to run the following linq query:
var entries = from entry in _db.Entries
select new CommentSummary()
{
NumberOfComments = entry.Message.Count(),
UserName = entry.Name
};
when I execute the query, it throws the mentioned error:
Message=DbExpressionBinding requires an input expression with a collection ResultType.
Parameter name: input
If I use
var entries = from entry in _db.Entries
group entry by entry.Name into groupedByName
orderby groupedByName.Count() descending
select new CommentSummary
{
NumberOfComments = groupedByName.Count(),
UserName = groupedByName.Key
};
there is no error, but the Comments are not counted correctly: all NumberOfComments values are "1", and there should be some "1" and some "0".
Any ideas? Thanks
you should use 'by new' after 'group'. I hope this will help you.
var entries = from entry in _db.Entries
group entry by new { entry.Name } into groupedByName
select new
{
groupedByName.Key.Name,
NumberOfComments = groupedByName.Count(x => x.Name != null)
};

Value cannot be null. Parameter name: source in MVC listbox

I have found plenty of posts about this, but none of them are solving my issue. My code right now:
#Html.ListBox("SelectedNewsletter", Model.Newsletters)
and
public MultiSelectList Newsletters
{
get
{
return new MultiSelectList(
new[]
{
// TODO: Fetch from your repository
new { Id = 1, Name = "item 1" },
new { Id = 2, Name = "item 2" },
new { Id = 3, Name = "item 3" },
},
"Id",
"Name"
);
// return new MultiSelectList(PromoNewsletter.All, "IdString", "Display");
}
}
As far as I can see, it's all hard coded now, and it still gives the same error. I want to do a ListboxFor, but I am trying to just get a listbox to work. I have replaced my int id with a string representation, based on advice I found elsewhere, but now I don't see what else I can do. It just plain is not working, even with all hard coded values and not binding to a property on my ViewModel. Where am I going wrong ?
The error is occurring because you have a property in the Model or ViewData/ViewBag with the name SelectedNewsletter.
Give a different name for the ListBox if you can't rename that property.
UPDATE:
After little more experimenting I figured out that the problem is you may be setting an integer value to the SelectedNewsletter that is somewhere in the ViewData/ViewBag or Model.
You can set the values that has to be selected in the ListBox as a string or array of strings to the SelectedNewsletter.
i.e SelectedNewsletter = "1";
or
SelectedNewsletter = new[] { "1", "3" };
You can also use strongly typed helper to make things easy,
#Html.ListBoxFor(m => m.SelectedNewsletter, Model.NewsLetters);

Pass a List of Series to SetSeries

I am using DotNet.Highcharts in conjunction with Visual Studio 2010. I have created an array of Series:
List<Series> allSeries = new List<Series>();
I then looped through a database and added several different Series. Then I created a Highchart and need to add the allSeries list to it. I have the code below that I used to create a Series one at a time. How can I take the allSeries list and pass it to SetSeries?
.SetSeries(new[]
{
new Series { Name = "Combiner 2", Data = new Data(myData2) },
new Series { Name = "Combiner 3", Data = new Data(myData3) }
});
if I am left to assume that the myData2 and myData3 objects are contained in or could be extracted from allSeries, then you should be able to do something like this:
.SetSeries(allSeries.Select(s=> new Series { Name = s.Name, Data = s.Data }));
EDIT:
If set series isn't looking for an IEnumerable<Series> but instead needs Object[] or Series[], then you could do this:
//casts series elements to object, then projects to array
.SetSeries(allSeries.Select(s=> (object)new Series { Name = s.Name, Data = s.Data }).ToArray());
or maybe this:
//projects series elements to array of series
.SetSeries(allSeries.Select(s=> new Series { Name = s.Name, Data = s.Data }).ToArray());
it all depends on what the method signature for SetSeries is.

if-else clause in Linq to entity framework query

Following Linq to Entities query is causing the "Unable to create a constant value of type 'Data.InhouseUnit'. Only primitive types ('such as Int32, String, and Guid') are supported in this context" exception.
IList<FaultReport> faultReports = (from fr in _session.FaultReports
where fr.CreatedOn > dateTime
select new FaultReport
{
Id = fr.Id,
ExecutionDate = fr.ExecutionDate ?? DateTime.MinValue,
FaultType = fr.FaultType,
Quarters = fr.Quarters,
InhouseSpaceId = fr.InhouseSpaceId,
InhouseSpace = new InhouseSpace { Id = fr.InhouseSpace.Id, Name = fr.InhouseSpace.Name },
InhouseUnitId = fr.InhouseUnitId ?? Guid.Empty,
**InhouseUnit = fr.InhouseUnitId == Guid.Empty ? null : new InhouseUnit { Id = fr.InhouseUnit.Id, Name = fr.InhouseUnit.Name }**
}).ToList();
Specifically, it is the if expression in bold font which causes the exception. I need to make the check as fr.InhouseUnitId is a nullable. If I take out the the bolded expression, the rest of the statement works just fine. I have spent a fair amount of time, in msdn forum and on web, to understand what is causing the exception but still cannot quite understand. Guid is scalar so it should work, right? Even this expression InhouseUnit = true ? null: new InhouseUnit() in place of the bolded expression in the above statement wouldn't work. Can we even write if/else
If i try to write an extension method to take away the logic and just return a result, following exception is thrown:
LINQ to Entities does not recognize the method 'System.Object
GuidConversion(System.Nullable`1[System.Guid], System.Object)' method, and this method
cannot be translated into a store expression
It looks like you are projecting into new objects of the same type that you are querying from. Is that the case? It seems a little weird, but assuming you have a good reason for doing this, you could split the query into two parts. The first part would get what you need from the database. The second part would run locally (i.e. LINQ-to-Objects) to give you the projection you need. Something like this:
var query =
from fr in _session.FaultReports
where fr.CreatedOn > dateTime
select new {
fr.Id,
fr.ExecutionDate,
fr.FaultType,
fr.Quarters,
InhouseSpaceId = fr.InhouseSpace.Id,
InhouseSpaceName = fr.InhouseSpace.Name,
InhouseUnitId = fr.InhouseUnit.Id,
InhouseUnitName = fr.InhouseUnit.Name,
};
IList<FaultReport> faultReports = (
from fr in query.ToList()
select new FaultReport {
Id = fr.Id,
ExecutionDate = fr.ExecutionDate ?? DateTime.MinValue,
FaultType = fr.FaultType,
Quarters = fr.Quarters,
InhouseSpaceId = fr.InhouseSpaceId,
InhouseSpace = new InhouseSpace { Id = fr.InhouseSpaceId, Name = fr.InhouseSpaceName },
InhouseUnitId = fr.InhouseUnitId ?? Guid.Empty,
InhouseUnit = fr.InhouseUnitId == Guid.Empty ? null : new InhouseUnit { Id = fr.InhouseUnitId, Name = fr.InhouseUnitName }
}).ToList();

Resources