Getting similar items from database - c#

I have Sql table that contains two fields Title and Description, I use LINQ to SQL for all datbase operations, how would I get similar items depending on the item that I am currently showing to the user, I know this can be done by using Full Text Search in SQL but LINQ to SQL doesn't support it.
Can someone explain me logic beheind this, or show me example how it's done? Should I split Title and Description on words and then search the table or? SO similar questions system seems perfect, how do they do it?

There is a function called Contains() , I think It will solve your problem , here is an example about how to use It :
db = new YourDataContext();
List<Table> list = new List<Table>();
list = (from i in db.Table where (i.Item.Contains(curentItem)))select i).ToList();

Related

What is the difference between these two LINQ Queries and how to correctly optimize them?

I have a Clients table which has the following columns:
Id, FirstName, LastName, CurrencyId, Gender.
I want to select the client's Currency that corresponded to Id 10 so I am doing something like this:
var currencyId = Db.Clients.FirstOrDefault(c=>c.Id == 10)?.CurrencyId;
Does this line bring all properties from Db and selects the currency in the code or it executes something like this in the database:
SELECT currencyId FROM Client WHERE ID = 10
Or I should write the linq like this:
var currencyId = Db.Clients.Where(c=>c.Id == 10).Select(c=>c.CurrnecyId).FirstOrDefault();
What's the difference between the two queries?
And what is the correct way to translate the above SQL query into a linq query?
Looked into it myself cause I found the anwser from most people questionable. I expect the FirstOrDefault to materialize the result (you also see from the type that you are not longer working with a query object), so that would mean it queries for all properties.
Unlike the 2nd query where you are still working with a query when filtering the property you like, thus dependent on the implementation it could be used for filtering properties and selecting specific fields.
The following is an example of the queries generated using EF for two similar queries, where it shows both generating different queries: https://dotnetfiddle.net/5aFJAZ
In your first example, var currencyId = Db.Clients.FirstOrDefault(c=>c.Id == 10)?.CurrencyId; the query selects the entire object to memory and then returns the Id property from that in memory object. As a result it needs to do something like the following SQL: SELECT * FROM Clients WHERE Id = 10. I understand I'm not using a parameter here and EF does spell out every column. However the key to understand here is that by returning more columns than you need, you potentially are setting up a performance concern because a covering index on Id and CurrencyId would not be used.
Your second LINQ query would use a SQL statement like SELECT CurrencyId FROM Clients Where Id = 10 which would take advantage of your indexes assuming you have an index covering these columns.

is there a better performance's way to get only few columns in ASP MVC without getting the whole table and filter it with LinQ?

im calling a table with 200.000 rows and 6 columns, but i only want 2 of these columns to be used in one controller, so i want to know if there is a better way to call them from the server without compromising performance, because as i know Linq queries get the whole table and them makes the filtering, i think maybe Views is a good way, but i want to know if there are others and betters, Thanks.
for example:
var items = from i in db.Items select new {i.id,i.name};
in case i have 1.000.000 items, will it be a trouble for the server?
Your initial assumption is incorrect.
In general LINQ queries do not get the whole table. the query is converted into a "server side expression" (i.e. a SQL statement) and the statement is resolved on the server and only the requested data is returned.
Given the statement you provided you will return only two columns but you will get 1,000,000 objects in the result if you do not do any filtering. But that isn't a problem with LINQ, that's a problem with you not filtering. If you included a where clause you would only get the rows you requested.
var items = from i in db.Items
where i.Whatever == SomeValue
select new { i.id, i.name };
Your original query would be translated (roughly) into the following SQL:
SELECT id, name FROM Items
You didn't include a where clause so you're going to get everything.
With the version that included a where clause you'd get the following SQL generated:
SELECT id, name FROM Items WHERE Whatever = SomeValue
Only the rows that match the condition would be returned to your application and converted into objects.

Query Expression Distinct column Dynamics CRM 2011

I have created a query expressions which retrieves all order products associated with an order.
Here is my current query expression:
var query = new QueryExpression("salesorderdetail");
query.ColumnSet = new ColumnSet(new string[] { "salesorderdetailid", "productid", "new_event", "new_inventory", "productdescription" });
query.Criteria.AddCondition("salesorderid", ConditionOperator.Equal, combinedEntity.Id);
query.Distinct = true;
EntityCollection retrieved = context.OrganizationService.RetrieveMultiple(query);
The problem is that I only want to retrieve data with a unique productid.
Is this possible using QueryExpression? Can anyone show me?
Many thanks.
I am not 100% sure what you are asking, however there is a trick I like to use that may help when writing query expressions:
Navigate to the view that displays the records in question
Click the Advanced Find button in the Ribbon Bar
Configure your “find” until it shows the records you are looking for
Click the Download Fetch XML button in the Ribbon Bar
Open the file with a text viewer (or your fav dev tool)
While this is not “code” per se, it usually contains some good hints on how to write it. Also, there is the option of using a Fetch XML query in your code as well.
Use FetchXML to Construct a Query
http://msdn.microsoft.com/en-us/library/gg328117.aspx
If you just need the distinct set of ProductIds contained within the sales order, couldn't just remove the other columns from the ColumnSet? Like so:
query.ColumnSet = new ColumnSet(new string[] { "productid" });

SQL Server : how to store and read product specifications (tags) to database

I need your help.
I am creating an eCommerce website and I hit a wall with product specifications.
I have the following tables
Items
ItemID (1231)
ItemName (Dell Inspiron)
Specs
SpecID (5342) (5343)
ItemID (1231) (1231)
SpecName (HDD) (MEM)
SpecValue (500GB) (4GB)
The values in brackets are table rows.
Is this the best way to store the specs in the database ?
How can I query it later ? Lets say I need all products with HDD=500GB and MEM=4GB ?
Is it possible to read it from database in C# (asp.net) as an object Items that has the sub object Specs ?
Is there a way to get all the specs combined in only one Items row grouped by ItemID ?
Thanks in advance.
What you're hinting at is called EAV (Entity-Attribute-Value). You can read the Wiki linked, and knowing what to look for should get you started to digg further and find C#/EF/Linq/ASP.Net solutions for specific EAV related questions.
Some good discussion will be right here, eg. Entity Attribute Value Database vs. strict Relational Model Ecommerce question.
The SQL Server CAT team has put forth the Best Practices for Semantic Data Modeling for Performance and Scalability, a must read.
Is this the best way to store the specs in the database ?
It looks like Item and Spec have a many-to-many relationship. So a better design would be like this:
Item(ItemId PK, ItemName)
ItemSpecs(ItemId, SpecId, SpecValue)
Specs(SpecId PK, SpecName)
How can I query it later ? Lets say I need all products with HDD=500GB and MEM=4GB ?
SELECT i.* FROM Items i, ItemSpecs is1, Specs s1, ItemSpecs is2, Specs s2
WHERE i.ItemId = is1.ItemId
AND is1.SpecId = s1.SpecId
AND s1.SpecName = 'HDD'
AND is1.SpecValue = '500GB'
AND i.ItemId = is2.ItemId
AND is2.SpecId = s2.SpecId
AND s2.SpecName = 'MEM'
AND is2.SpecValue = '4GB'
However, normally you would already know the SpecId you're querying for, so you won't need the joins on Specs.
Is it possible to read it from database in C# (asp.net) as an object Items that has the sub object Specs ?
I'm not sure what you mean by this.
Is there a way to get all the specs combined in only one Items row grouped by ItemID ?
Only if you know the number of specs in advance or if you dynamically construct your query. The more specs, the more columns the row will have.

Can I improve read only perfomance using View on my DataBase and EF?

I use SQL 2008 Linq and Entity Framework 4.
In my DataBase I have a table called CmsContents.
This table has many fields like Title, CategoryId and some pretty heavy like HtmlCode.
In many part of my Front end I need display only some field of my table CmsContents like example only the Title and CategoryId in descent order.
Question:
Can I improve read only performance using a partial View for my Table CmsContents (that exclude the filed: HtmlCode)?
So I would map this View to my Model in EF and query with Linq to get the desidred result in the front-end.
My aim is to improve performance and avoiding working with AnonymousType in my Linq to Entity queries.
Let me know what do you think.
Thanks for your help on this.
You can select only the fields you require, that way you would not need to load un-necessary data. It does not have to be anonymous.
Example:
var result = from c in dbContext.CmsContents select new CmsContent
{Title = c.Title, CategoryId = c.CategoryId}
Edit: please try this on your code, this should work if your .CmsContents is IEnumerables, which I think it is.
var contents = context.CmsGroupsTypes
.Single(g => g.GroupTypeId == 1)
.CmsContents
.Select(cms=>new CmsContent{Title = cms.Title, CategoryId = cms.CategoryId})

Categories

Resources