I have a model named EmailDetailModel with a Dictionary field.
My query is:
var query = from email in context.EmailModel
where rolesForUser.Contains(email.GroupName)
orderby email.Id descending
select new EmailDetailModel()
{
From = email.From,
Id = email.Id,
message = email.Body,
subject = email.Subject,
pathFileName =
(from pdf in context.PdfModel
where pdf.Email_Id == email.Id
select pdf)
.ToDictionary(k => k.PathFile, v => v.FileName),
timeSend = email.ActiveTime};
However an error occurs on this line when trying to send the list to the view:
View(query.ToPagedList(pageNumber, pageSize));
Saying that:
LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary
If anybody could help me, I'm not sure how I can pass the key and value into my pathFileName Dictionary.
The issue likely is that there is no SQL like conversion of the dictionary generation that LINQ to Entities can understand.
I have struggled with similar error messages whenever LINQ cannot be translated to a SQL statement.
Related
In EFCore5, implicit tables are saved as Dictionary<TKey, object> sets, knows as Property Bag Entity Types. However, I cannot figure out how to create a LINQ query with a Where() clause that compiles for MySQL for such a property bag entity type.
For example, this code successfully retrieves the IQueryable reference to an intermediate table (generated by EFcore5's implicity many-to-many feature) given the ISkipNavigation:
ISkipNavigation nav = // some many-to-many relationship
IQueryable<Dictionary<string, object>> intermediateTable =
context.Set<Dictionary<string, object>>(nav.JoinEntityType.Name);
And this code successfully retrieves all the entries in the intermediate table:
List<Dictionary<string, object>> joins = await intermediateTable.ToListAsync();
In the resulting List, each Dictionary has just one key/value (representing the row).
However, I cannot figure out how to add a .Where() clause to the LINQ query which will compile:
joinTable.Where(d => d.Keys.First() == "foo").ToList();
joinTable.Where(d => d.Keys.Any(k => k == "foo")).ToList();
The error is:
Translation of member 'Keys' on entity type 'MachinePartMachineProfile (Dictionary<string, object>)' failed. This commonly occurs when the specified member is unmapped. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'
I do not wish to do client-side parsing for performance reasons (the join table will be to big).
I note that the type reported by the error is MachinePartMachineProfile (Dictionary<string, object>). Some investigation showed that these types are being generated based upon the static Model.DefaultPropertyBagType (which is a Dictionary<string, object>). But despite staring at the EFCore code base, I cannot discern how to correctly query such a default property bag type.
I am using MySQL as my database, if it is relevant.
You can index the dictionary directly, with knowledge of the column name.
Working example would be:
joinTable.Where(d => d[columnName] == "foo").ToList();
And for the sake of completeness, if you have an ISkipNavigation instance, you can infer these keys as follows:
string foreignKey = nav.ForeignKey.Properties.First().GetColumnBaseName();
string localKey = nav.Inverse.ForeignKey.Properties.First().GetColumnBaseName();
I have the following objects on cosmosdb
And my code with cosmonaut library its like this:
var pageTemplate = await pageTemplateStore.Query().FirstOrDefaultAsync(x => x.Id == id);
where id its either 0 or 1, but I am always getting null as a result
The problem is that you haven't decorated your object's Id field with [JsonProperty("id")] so the built in LINQ to SQL translator can't convert it. It can be found in Cosmonaut's documentation
However you should not query for documents using their id. This will perform a query instead of a read an it will cost way more RUs and more time. Use the FindAsync method instead which will do a direct read and will only cost 1RU.
I am using below code for retrieving some object based on creation date
String query = ContentCleaningUtil.buildQuery(fileNamePattern, uTCTimeString);
OperationContext opCon = session.createOperationContext();
opCon.setLoadSecondaryTypeProperties(true);
opCon.setMaxItemsPerPage(1000);
ItemIterable<QueryResult> results = session.query(query, false, opCon);
I am getting empty results.
The query String is:
SELECT * FROM cmis:document WHERE cmis:name LIKE '%.zip' AND cmis:creationDate > TIMESTAMP '2018-09-01T22:56:44.248Z'
But when I am firing same query in alfresco node browser setting query as cmis-alfresco, it is returning 2 desired objects.
In both case I am using store workspace://Spacestore
Can anyone please help on this.
I have this code that uses a raw SQL query inside my controller:
$sql1 = "SELECT completion_date FROM enviro_figures_upload GROUP BY YEAR(completion_date), MONTH(completion_date) DESC;";
$activeDate = $this->getDoctrine()->getManager()->getConnection()->prepare($sql1);
$activeDate->execute();
$activeDate->fetchAll();
This code then passes the data to the view which is then used in a drop down date picker. However, no results are passed to the view even though running that SQL query on the database returns the results I need. What am I missing in order to pass this data to the view?
$activeDate->execute();
$activeDate->fetchAll();
This code then passes the data to the view ...
this code doesn't pass the data to view, you have to pass the data to view by array option in render method..
something like this:
$sql1 = "SELECT completion_date FROM enviro_figures_upload GROUP BY YEAR(completion_date), MONTH(completion_date) DESC;";
$activeDate = $this->getDoctrine()->getManager()->getConnection()->prepare($sql1);
$activeDate->execute();
$result = $activeDate->fetchAll();
return $this->render('TEMPLATE_PATH', [
'result' => $result
]);
Hi i have written one linq query to fetch records from entity model. I am getting perfect number of records but all are same.
here is my query
Entities.TEST.Where(a => a.ID.ToUpper().Equals(ID.ToUpper())).OrderBy(s => s.NAME).ToList();
Am I missing something?
You need to make sure your Entity Key in your Entity Data Model is unique.
So in your example, ID should be the entity key for your Test entity
Your query should work, i have a similar sample that works for northwind DB:
var ctx = new NorthwindEntities();
var emp = ctx.Employees.Where(e => e.TitleOfCourtesy.Equals("ms.", StringComparison.OrdinalIgnoreCase)).OrderBy(n => n.FirstName).ToList();
Please check your query in LinqPad. You will see the results and the generated SQL.
Replace Equals with == and you can go