I have a entity : one Question has many answers.
to use JSTL show the results conveniently, I'd like to combine many select results to only one entity; just like the following:
<!-- 根据节查找 -->
<select id="selectDetailedBySectionId" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
q.id, q.question, q.type, q.tea_id,
a.id as ans_id, a.item as ans_item, a.isTrue as ans_isTrue
from questions q, answers a
where q.sec_id = #{id} and q.id = a.que_id
</select>
I give a select ,it back to 4 results.so if i us the frontPage is like this:
so is there a method to combine these entity to just one.
From what i understand, you want a result entity consisting in: 1 question => a collection of N answers.
You will have to use Mybatis collection mapping, see an example here.
Related
An Invoice has many products and a product can have many X (X shall not be important here)
var query = from i in invoices
join prod in i.products on i.id equals prod.InvoiceId
select new MyClass{ Id = i.id, more props }
Why does the join not work?
I get the error that "i.product" The "i" here does not exist in the current context.
join is for when you have two separate collections, where you want to compare some properties of the various elements of those collections. You can use it when you have a separate unfiltered collection of products, and you want to find products that match on invoice ID. For instance, you might use it if you didn't define any navigation properties.
That's not what you're after here: you do have a navigation property, and by using that, Entity Framework will have set up things so that i.products only contains the matching products. You don't need to link on invoice ID manually.
var query = from i in invoices
from prod in i.products
select ...;
The question is quite mouthful , it can be simply described by the following query:
select c.name,c.brands FROM ShopChannels c
ShopChannel is the basic entity which has a collection of brands which is of on-to-many,instead select all the brand properties in this query ,I only want to select partial fields of Brand, if I use
select c.name,c.brands.spell from ShopChannels c
I'll get a "cannot navigate collection valued association field " error message.
This kind of query can greatly boost performance, so anyone has an idea?
The JPA specification says that "´It is syntactically illegal to compose a path expression from a path expression that evaluates to a collection.´" A solution to your need is a join query like: ´select c.name,b.spell from ShopChannels c inner join c.brands b´.
Is it possible to include a computed field in an EF entity? Example, lets say I have a shop selling products, and the products are grouped into categories.
When the list of categories is displayed for editing to the administrator of the shop I wish to list the number of products in each category as part of the list.
In NHibernate I would use a formula e.g.
<property name="Products" formula="dbo.Category_NumProducts(id)" />
I can't work out if something similar is possible for EF. I know I could make a category view and map this to a second entity but that seems wrong as its almost a complete duplication.
EDIT: If this isn't possible in EF, then what is the best way to accomplish this?
Unfortunately if your Category table doesn't have this as computed column you will not be able to map it without creating second entity - it leads to database view, defining query in EDMX or perhaps Query view in EDMX (but Query view may not work because you require aggregate function to be computed).
Edit:
IMHO the simplest solution (and also the best) is simply creating a ViewModel (some non mapped class) and use projection in Linq query:
var query = from c in context.Categories
where ...
select new CategoryView {
Id = c.Id,
Name = c.Name,
...
Products = c.Products.Count()
};
I want to perform a selection by 2 Entity ( AppRoles and AppUsers) ,
If in SQL, i would do this, For example :
SELECT u.*, r.* FROM AppUsers u ,AppRoles r WHERE u.RoleID = r.RoleID
Also it can be done using LINQ Syntax in Code-Behind.
However, i don't know how to do in EntityDataSource
Below is my mark-up :
<asp:EntityDataSource ID="edsUsers" runat="server"
ConnectionString="name=ReferralDBEntities"
DefaultContainerName="ReferralDBEntities" EnableFlattening="True"
EntitySetName="AppUsers"
Include="AppRoles"
Select="it.AppUsers, it.AppRoles"
Where="it.AppUsers.RoleID = it.AppRoles.RoleID"
>
</asp:EntityDataSource>
But it show the error.
assuming you want a list of all users in each role
and assuming you have a 3 table structure i.e. users, UserInRoles, UserRole
Then
<asp:EntityDataSource ID="edsUsers" runat="server"
ConnectionString="name=ReferralDBEntities"
DefaultContainerName="ReferralDBEntities" EnableFlattening="True"
EntitySetName="UserInRoles"
Include="UserRole, User"
Select="it.User.FullName, it.UserRole.Name">
</asp:EntityDataSource>
Job done
An older post not answered.
Try removing the Select="it.AppUsers, it.AppRoles"
Otherwise I am not sure how the relationship between your two tables is setup in the EDMX. You may want to check those also.
I found several times people asking for the same question but it seems that the answer was never satisfying altough it should be pretty easy (in theory). Here is my question :
I have an entity called "Company" inside which I have an entityCollection "Employees" (one to many). I need to retrieve all Companies and for each of them, I only want the employees with an Age greater than 21.
I tried :
Return context.Companies.Include("Employees").Where(c => c.Employees.Where(e => e.Age > 21).Count() > 0)
That doesn't work as it gives me all employees for each company if there is at least one above 21 (it is actually the same than .Any() )
I tried :
Return context.Companies.Include("Employees").Select(c => New Company {
.Id = c.Id,
.Employees = c.Employees.Where(Function(e) e.Age > 24)
}).ToList()
That didn't work either (although it would have been perfect), it gives me the following error : The entity or complex type 'MyModel.Company' cannot be constructed in a LINQ to Entities query.
How can you select all my companies with only, for each of them, the employees being above 21 ? At the moment, I select all and on the client side, I filter my employees but I don't like that solution.
Can anybody help me ?
Thank you Morteza Manavi-Parast, it will do the work !
Nevertheless, I hardly convince myself that doing so in a unique query has not be implemented in the Entity framework. It is such a relatively common situation ... As a prove, there are numbers of questions like mine on this forum.
I am surprised ... Maybe for the next release ?
To be clear, I need a list of Companies as I am directly binding the result of my query to a datagrid. For your information, when I click on a row of my datagrid (so selecting a company), I have a second Grid which is populated with its employees (above 21 years old) coming from the entityCollection.
There is no way to have a "Conditional Eager Loading" with include in LINQ to Entities. There are 2 workarounds exist though. The first one is Filtered Projection and it's the one that Justin suggested but might not be desirable in all situations as it gives a collection of anonymous type objects.
The second way is called Two Tracked Queries which gives you a collection of strongly types Companies whose their employees satisfy a condition and I believe that's what you are looking for. Here is the code for it:
var companies = context.Companies.ToList();
var employees = context.Employee.Where(e => e.Age > 21);
foreach (var employee in employees) {
companies.Single(c => c.CompanyID == employee.CompanyID).Employees.Add(employee);
}
Please take a look at Conditional Eager Loading for another example.
Instead of using the type Company, have you tried selecting a new anonymous type:
Return context.Companies.Include("Employees").Select(c => New With {
.Id = c.Id,
.Employees = c.Employees.Where(Function(e) e.Age > 24)
}).ToList()
(Sorry if the syntax is a little off, it's been a while since I've done LINQ/Anonymous Types in VB.NET)
You might be over-thinking this one. If you have the Company => Employee relationship bi-directionally mapped, then just do the select on Employee with the where clause and include company.
Return context.Employee.Include("Company").Where(e => e.Age > 21)