How to query flattened relational data in JavaScript - tsql

I’m calling a web method in asp.net from an Axios Ajax get request.
The web method uses LINQ to join the data from three tables in the adventureworks database - categories, sub-categories and products.
This works great, but how would I use three nested selects - categories, sub-categories and products in JavaScript to query what is now a flat table - an array of objects ?

Related

Laravel eloquent join multiple table and search data using with() method

I have two table user and user_info. I need to join those table and have to search data from them. It is throwing error as unknown column.I have solution using DB query, Is it possible to do search using with() method in controller and eloquent relationship in model.
Thank you
It's not possible to filter models by their related models attributes using with() - this method only allows filtering related models, not the original ones you're loading.
In order to filter by attributes of related models you should use whereHas() method, e.g. in order to load all users that have country column set to uk in their user_info data you could do the following:
$usersFromUK = User::with('user_info')->whereHas('user_info', function($query) {
$query->whereCountry('uk');
})->get();

Entity Framework table per type - select from only the base type columns

We are using EF 4.3 Code first and have an object model like so:
class Content { }
class Product:Content { }
class News:Content { }
These are mapped as Table per Type.
There are scenarios where I just want to load only the columns belonging to the base table, like say a list of all the content titles. But a query like
from c in Content
where c.IsDeleted == false
select c
results in some really nasty SQL with joins to the other two tables. Is there any way to force EF to just do a select from the base table only without joins to the other tables?
TPT is problematic and EF generated queries are usually very inefficient. Moreover your expectations are probably incorrect. Linq-to-entities always returns the real type of entity. It cannot return instance of Content type if the record is in fact a Product entity. Your query can have only two meanings:
Return all non deleted contents - this must perform joins to correctly instantiate a real types of entities. The query will return enumeration of Content, Product and News instances.
Return all non deleted Content instances - this must probably again perform joins to correctly instantiate only records mapped to Content directly (without relation to Product and News). No record mapped to Product or News will be returned in the enumeration. This query is not possible with Linq-to-entities - you need to use ESQL and OFTYPE ONLY operator.
There are few things you can try:
Upgrade to .NET 4.5 - there are some improvements for TPT queries
Return projection of properties instead of Content - Product and News are also content so you will never get query without joins if you return Content instances from Linq-to-entities query

Joining list with table in Entity Framework

I am using Entity Framework with asp.net mvc3 razor. Now I have a table which represents Countries (like India, US) etc. And my requirement is I need to open a pop-up with the flags of all countries which I have in my database. And when user click on one flag I need to show that particular country details first and remaining as line by line in the webgrid(asp.net mvc3 razor)
So I prepared a list of "Countries" by getting all the countries from database. And I prepared another list OrderofCountries by adding Order(property) "1" to the which user had clicked. And from 2 to all the remaining countries. Now I need to join this list i.e "OrderofContries" with the remaining tables (from database as for the requirement). But Entity Framework raises an error:
Unable to create a constant value of type 'Slmg.BusinessObjects.CountriesBO'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
My idea here is by using order property I can sort the data so that I can get the required data.
Can we join our prepared list with the database table in Entity Framework? How to solve my issue. Can any one please help me to find the solution.
No you cannot join list in your application with database table unless you pull all data from that table to your application by calling ToList or AsEnumerable.

JPA/Eclipselink - Multpile entities in single table

I'm using Eclipselink to map my tables to entities.
I have one big database table (actually it's view) with columns like groupId, groupName, categoryId, categoryName etc. I know it's redundand, but we're trying to minimize queries and it's dynamically created view.
The question is: How to map such table to several entities like Group, Category etc?
You would probably be better off mapping to the real tables and use query optimization to reduce your queries (such as join fetching and batch fetching)
See,
http://java-persistence-performance.blogspot.com/2010/08/batch-fetching-optimizing-object-graph.html
If you really want to have several class map to the same table, you will need to have one Entity and make the rest Embeddables.
See,
http://en.wikibooks.org/wiki/Java_Persistence/Embeddables

Trying to traverse tables from sql database using entity framework targeting .net 3.5 websites

I'm simply trying to get data from two sql server db tables using ado.net entity framework. My code is:
using (Model.Entities e = new Model.Entities())
{
return e.PAGE.First().CONTROL;
}
The database is setup to have two tables, a control table which links to a page table via an 'id' field in the tables (control_id). There is one CONTROL object for each PAGE object.
I keep getting a null value for my return value and I know that's not right.
I can use vis studio and breakpoints to see that there is a PAGE object in 'e' and I can see that there are multiple CONTROL objects in 'e'. This isn't a large database, I just have some sample data in there to ensure that I get this working - so I know that there should be a CONTROL object connected to this PAGE (i've verified this through sql server).
I am very familiar with the general code syntax, I've been using LINQ for a couple of years; however, I have not done much work at all with the entity framework or ado.net 4.
It seems like if I just pull individual table data then it works fine (i.e. e.PAGE.First() .. or .. e.CONTROL.Where(x=>x.someValue.Equals('someValue') ) but if I try to pull by traversing through the tables then I get nothing back (NULL).
Hope that all makes sense.
Some questions for you:
I assume is a 1..1 between PAGE and CONTROL,
Is there a FK called "ControlID" on PAGE?
Do you have a navigational property called "Control" on your "Page" entity in your EDMX?
If the answer to all of the above is Yes, then this should work:
var page = e.Pages.Include("Control").First();
Here, you are returning the First "Page" record, and eager loading the associated control.
The SQL produced should be something like this:
SELECT p.*, c.*
FROM Page p
INNER JOIN Control c
on p.ControlId = c.ControlId