Nest ElasticSearch cannot convert Lambda expression - nest

The following VB code is intended to add an alias index to a nest elastic search but the final line receives an error -
"lambda expression cannot be converted to NEST.IAliasRequest because Nest.IAliasRequest is not a delegate type"
Dim client = ConnectToSearch()
Dim Person = New Person With {.Id= Id, .Description = Description, .Tags = Tags}
client.Alias(Sub(a) a.Add(Function(add) add.Index("Person").Alias("Account1")))

client.[Alias](Function(descriptor) descriptor.Add(Function(a) a.Index("Person").[Alias]("Account1")))
UPDATE
Maybe this one will help you create alias with filter.
client.[Alias](Function([alias]) [alias].Add(Function(a) a.Index(indexName).[Alias]("alias").Filter(Of Person)(Function(f) f.Term("relationships.staffID", staffID))))
Hope this helps.

Related

The ""sort"" value has to be formed as this example: ""field_ASC""

I'm developing an integration between a ERP and a Prestashop store, I'm now trying to use a product filter to check if a product already exists, so i can either add a new product or update an existing one.
When I try to get the filter result i got a XML response with the following error message
<![CDATA[The ""sort"" value has to be formed as this example: ""field_ASC"" or '[field_1_DESC,field_2_ASC,field_3_ASC,...]' (""field"" has to be an available field)]]>
I didn't understand what that really means, I have searched around a while and coudn't find any specific problem like this and all the examples I've found are the same.
Here is my code, it is in VB and I'm using Bukimedia Prestasharp
Dim oProdFac = New ProductFactory(oParameters.UrlAPI, oParameters.TokenAPI, oParameters.KeyAPI)
Dim objFiltro = New Dictionary(Of String, String)
Dim listFilterProducts As New List(Of Bukimedia.PrestaSharp.Entities.product)
objFiltro.Add("reference", "My_Product_Id")
listFilterProducts = oProdFac.GetByFilter(objFiltro, "null", "null")
What am I doing wrong?
Allright, the answer is SO SIMPLE that i felt ashamed.
I was sending incorrect parameters in the GetByFilter function.
I've changed this
listFilterProducts = oProdFac.GetByFilter(objFiltro, "null", "null")
to this
listFilterProducts = oProdFac.GetByFilter(objFiltro, "reference_ASC", "null")
Just like the xml message said. It solved the problem.

FindAll() in Linq Query List

I have a Linq Query made into a list called "ticket query"
I want to search ticket query for all the records that have specific data
I tried using FindAll() but it gives me an error
Argument matching parameter 'match' cannot convert from
'VB$AnonymousDelegate_1(Of JobPartForm,Nullable(Of Boolean))' to
'Predicate(Of JobPartForm)'.
I can't do the findall directly in the query because its being called at a separate time
is there another way to accomplish this, or am I using find all wrong?
ticketquery = (From ticket In dbContext.JobPartForm
Select ticket).ToList()
Dim formticket = ticketquery.FindAll(Function(f As JobPartForm) f.JobNum = ticketnum And f.FormNumber = formnum)
You can do the same using IQueryable<TSource>.Where method:
Dim formticket=dbContext.JobPartForm.Where((Function(f As JobPartForm) f.JobNum = ticketnum And f.FormNumber = formnum)).ToList();
The first thing is try to never call ToList extension method from a DbSet, that will load your entire table to memory, is really inefficient and more when you can filter your data on the server side.

Cayenne Query Expression -- Need clarification

In the Apache Cayenne documentation, they provide an example of how to create a parameterized query using the Expression class' fromString() function:
// create a qualifier with two named parameters: "pname" and "aname"
Expression qual = Expression.fromString("paintingTitle = $pname or toArtist.artistName = $aname");
// build a query prototype of a query - simply another select query
SelectQuery proto = new SelectQuery(Painting.class, qual);
The making of such a query is pretty straightforward, except for one problem: the documentation does not explain what $pname and $aname are or how to set them to the values you want to query for!
Can anyone explain how to set these parameters??? Please advise...
You are probably checking older documentation. Check out "Named Parameter Expressions" here if you are on Cayenne 3.0, or "Creating Expressions from Strings" here for 3.1. But in any event, this is fairly simple - you put your parameters in a Map, and then use "expWithParameters" method. To follow your example:
Expression qual =
Expression.fromString("paintingTitle = $pname or toArtist.artistName = $aname");
Map<String, Object> params = new HashMap<>();
params.put("pname", "A");
params.put("aname", "B");
qual = qual.expWithParameters(params);
Note that in the last line I am reassigning the Expression, as 'expWithParameters' creates a clone.

Using contains on an integer in LINQ to SQL

I'm trying the following query:
Return _context.companies _
.Where(Function(c) c.id.StartsWith(filterCompanyId)) _
.ToList()
but because id is an integer, it doesn't work. I found this answer which suggests I can use
c.id.ToString().StartsWith ...
but that gives me the following error:
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
When trying to use
.Where(Function(c) SqlFunctions.StringConvert(CDbl(c.id)).StartsWith("1"))
it doesn't work.
Any tips?
The relevant part is hidden in the error message:
'SqlFunctions' is not declared. It may be inaccessible due to its protection level
SqlFunctions is in the System.Data.Objects.SqlClient namespace (you can discover this by going to the "SqlFunctions Class" MSDN page and then checking the "Namespace:" entry). Thus, you need to import that namespace at the top of your code file:
Imports System.Data.Objects.SqlClient
Per #Heinzi's comment, StringConvert is not supported by the MySQL-EF-Provider. I ended up going with a query string:
Dim isExpiredInt As Integer = If(isExpired, 1, 0)
Dim query As String = "Select * FROM company WHERE (expired = 0 OR expired = {0}) AND name like {1} AND id like {2}"
Dim companies = _context.ExecuteStoreQuery(Of company)(
query,
"companies",
System.Data.Objects.MergeOption.AppendOnly,
isExpiredInt, "%" & filterCompanyName & "%", filterCompanyId & "%"
).ToList()
Return companies
Note the System.Data.Objects.MergeOption.AppendOnly is something I added so the objects from this query would be attached to my context (i.e. calling SaveChanges on this context would write to the database).
Hope that helps!

Adding related records in LINQ

Processing an XML file with LINQ to add records into a table in a SQL Server database via a data context. As we are adding records we need to assign the parents in various other tables. Most of the time we can find the existing parent and use it but sometimes we will need to create a new parent.
Thought we could do this like this:
Dim defaultPub As publication
defaultPub = New publication With {.name = e..Value}
Dim pub = _Data.publications.Where(Function(s) s.name = e..Value).DefaultIfEmpty(defaultPub).SingleOrDefault
.publication = pub
So we are trying to find a publication that matches e..Value from our XML, but if we can't find one then we use 'defaultPub'. If it does exist though we don't want to add it again. So maybe this approach is flawed anyway even if it did work...
Anyway it's not currently working, we get this error:
Unsupported overload used for query operator 'DefaultIfEmpty'.
The overload requires a publication and is getting one (I've checked the TypeNames in quickwatch), don't know what is going on here.
What we are really looking for is something like the find_or_create from activerecord for Ruby that LINQ seems to be a copied from.
Thanks in advance,
Dave.
OK, I can just do it like this:
Dim pub = _Data.publications.Where(Function(s) s.name = e.<source>.Value).SingleOrDefault
Dim newPub As publication
If Not IsNothing(pub) AndAlso pub.name <> "" Then
.publication = pub
Else
newPub = New publication With {.name = e.<source>.Value}
.publication = newPub
End If
Thanks Val!