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
Related
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 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.
Hi is there an easy way to get the values of a foreign key, without writing sql queries.
I generated the code with the help of my mssql database (ADO.NET).
Here's an example for clarification
order table:
id customer_fk
1 100
2 105
customer table:
id name
100 Walter
105 White
view:
#model ...order
...
#customer_fk
#customer_fk delivers eg. "100" instead of "Walter"
Not sure if you are required to use ADO.NET, but to accomplish what you want, without writing sql, you'll need to use some kind of ORM, such as EntityFramework.
You will need to write LINQ, which generates SQL and since EF will know about the relationship between the two tables, you will have access to the "name" property in the customer table.
Linq to SQL might be a good option, since you're using MS SQL. Just add the DataContext item to your project, and drag/drop the tables from your server. Then you should be able to write something like:
public ActionResult Order(int orderId)
{
using (MyDataContext context = new MyDataContext())
{
var loadOptions = new LoadOptions();
loadOptions.LoadWith<order>(o => o.customer);
context.LoadOptions = loadOptions;
var theOrder = context.orders.Where(order => order.id == orderId).FirstOrDefault();
return View(theOrder);
}
}
But if you're already using ADO.Net, maybe just write the query? It's not that difficult:
SELECT order.*, customer.name
FROM order INNER JOIN customer ON order.customer_fd = customer.id
If you're doing this in Entity Framework (or some other similar ORM), you can write a LINQ query that joins to your foreign key table:
var customerOrders =
from o in context.Orders
join c in context.Customers on o.customer_fk equals c.ID
select new { OrderID = o.ID, CustomerName = c.Name };
I am working with entityframework 4.0. I have used conatins query for a entity table to check agains the list of ids. e.g
List<int> lstIds = lstComapreObjaects.select(c => c.Id).ToList();
IQueryable<ResultedEntity> qry = from su in Context.ResultedEntites
where lstIds.Contains(su.Id ?? 0) == true
List<ResultedEntity> lst = qry.ToList();
This works fine but the problem is it takes around 3-4 mins to execute the query...
So my question is how to improve the performance???
can i make join between entity and a generic list???
I have a request to create an auto complete that will search an data table. Is this achieveable quickly and simply or is it a case of writing a reasonable amount of code?
Originally, I have been using a webservice and linq to point at a single column's worth of data (IDDesc) and pull back the list of products:
Product.FinalProductsDataContext dbac = new Product.FinalProductsDataContext();
return dbac.tblProduct
.Where(r => r.Account== HttpContext.Current.Session["AccountKey"].ToString() && r.IDDesc.Contains(prefixText))
.Distinct()
.OrderBy(r => r.IDDesc)
.Select(r => r.IDDesc)
.Take(count)
.ToArray();
However, if I wish the autocomplete to look at all the columns, is it a case of repeating similar LINQ statements for each of the columns contained within the datatable or is there a 'quick fix'?
I personally don't think this is an ideal scenario but it is a request I must work towards.
Any help or advice, greatly appreciated.
Rather than trying to solve this entirely with LINQ (and repeating all those statements for each column in the table, as well as hitting the database repeatedly), I think I'd look to put something in the database to do the heavy lifting here.
You could create a view that takes the fields from the table and amalgamates them into one column e.g.
CREATE VIEW [dbo].[ProductView]
AS
SELECT CAST(ProductName AS NVARCHAR(50)) AS 'ProductColumn'
FROM dbo.Products
UNION
SELECT CAST(SupplierName AS NVARCHAR(50))
FROM dbo.Products
UNION
...
which if you added the view to your context would then allow you to modify your existing LINQ query and point it at that view e.g.:
Product.FinalProductsDataContext dbac = new Product.FinalProductsDataContext();
return dbac.ProductView
.Where(r => r.Account== HttpContext.Current.Session["AccountKey"].ToString() && r.ProductColumn.Contains(prefixText))
.Distinct()
.OrderBy(r => r.ProductColumn)
.Select(r => r.ProductColumn)
.Take(count)
.ToArray();