I have 4 tables:
User:
User_id
List item
User_Name
Company_id
Depart_id
Group_id
Company:
Company_id
Company_Name
Depart:
Depart_id
Depart_Name
Group:
Group_id
Group_Name
I use Entity Framework, how can I get User information:
User_id
User_Name
Company_Name
Depart_Name
Group_Name
Like this:
var va = from vx in dbContex.User
join vy in dbContext.Company
on vx.CompanyId equals vy.Company_id
into a
from b in a
select new {
vx.User_id,
vx.User_Name,
CompanyName = b.Company_Name
};
One solution could be:
var detailedUserInformations = (from u in dbContext.User
join c in dbContext.Company on u.Company_id equals c.Company_id
join d in dbContext.Depart on u.Depart_id equals d.Depart_id
join g in dbContext.Group on u.Group_id equals d.Group_id
select new
{
UserId = u.User_id,
UserName = u.User_Name
CompanyName = c.Company_id,
DepartName = d.Depart_Name,
GroupName = g.Group_Name
}).ToArray();
You have to check if a .Distinct() makes sense before you will project (.ToArray()) your selection.
Related
i have two tables : productprice and product
and i have a field name id on that tables, i would like update field name enddate on productprice base on code on product i try several syntax:
update productprice
set enddate = ’2016-12-31 00:00:00’
from product inner join
productprice
on product.id = productprice.id
where product.code = ‘9301940252’
but the result is "table name “productprice” specified more than once"
What I am doing wrong here? Thanks.
This is the correct syntax for MySQL:
update productprice pp join
product p
on p.id = pp.id
set pp.enddate = '2016-12-31'
where p.code = '9301940252';
Given your error and the fact that the question originally had Postgres and MySQL as a tag, perhaps you want Postgres syntax:
update productprice pp
set enddate = '2016-12-31'
from product p
where p.id = pp.id and p.code = '9301940252';
I'm new to postgresql. I'm looking to return 2 result sets, in my query, is this possible? My current query is using 'UNION ALL' where i'm getting dupplicate data.
WITH logged_in AS (
select exists (select 1 from users where user_name = 'z' and password = 'abc') AS logged_in, user_name, user_type
from users
WHERE user_name = 'z'
AND password = 'abc'
)
select logged_in, up.user_type, up.page_name, 'page' AS type
from logged_in AS l
JOIN user_application_mappings AS up
ON l.user_type = up.user_type
UNION ALL
select logged_in, fm.user_type, fm.feature_name, 'feature' AS type
from logged_in AS l
JOIN user_feature_mapping AS fm
ON l.user_type = fm.user_type
i just want my query to return the following:
**resultset 1:**
ID, user_name, logged_in, user_type
1 userA true admin
**resultset 2:**
pageName, Description, type
feature a xx page
feature b xx feature
how can i achieve this?
thanks
I have two table
Teachers(**int IDT**, int mat, String name ) and Courses(String name, **int IDT**). IDT is FKey in Courses and PKey in Teachers.
Then the teacher cant are in more that 3 courses. My query work fine in sql. My question is, 'How write this in LINQ to EF'?
select p.name, p.mat, p.IDT, count(c.IDT) from Teachers p
left join courses c on c.IDT = p.IDT
group by p.name, p.mat, p.IDT
having count(c.IDT) <3
You can try as shown below.
from p in context.Teachers
join c in context.courses on c.IDT equals p.IDT into j1
from j2 in j1.DefaultIfEmpty()
group j2 by new { p.name, p.mat, p.IDT } into grouped
let theCount = grouped.Sum(e => e != null ? 1 : 0)
where theCount < 3
select new { Name = grouped.Key.name, Mat= grouped.Key.mat,
Idt=grouped.Key.IDT,Count = theCount }
I have the classical person -> person attributes scheme.
So, like this: person(PK) <- person_attribute(FK)
What I need is a query to get one row where a person is joined with her attributes.
For example, to transform:
Person:
{ ID = 123456, Name = 'John Smith', Age = 25 }
Attributes:
1) { PersonID = 123456, AttributeTypeID = 'Height', AttributeValue = '6'6'''}
2) { PersonID = 123456, AttributeTypeID = 'Weight', AttributeValue = '220lbs'}
3) { PersonID = 123456, AttributeTypeID = 'EyeColor', AttributeValue = 'Blue'}
To:
PersonWithAttributes
{
ID = 123456, Name = 'John Smith', Age = 25, Height = '6'6''', Weight = '220lbs', EyeColor = 'Blue'
}
To make things worse my persons are in a table variable.
So, I have (in a sp with a parameter of person_id):
--result table
declare #people_info table
(
person_id int,
name nvarchar(max),
age int,
height nvarchar(10) null,
weight nvarchar(10) null,
eye_color nvarchar(16) null
)
insert into #people_info
select person_id, name, age, null, null, null
from dbo.HR.people where person_id = #person_id
update pi
set
pi.height = (select pa.attribute_value where pa.attribute_type_id = 'Height'),
pi.height = (select pa.attribute_value where pa.attribute_type_id = 'Weight'),
pi.eye_color = (select pa.attribute_value where pa.attribute_type_id = 'EyeColor')
from
#people_info pi
inner join dbo.HR.person_attributes pa on pi.person_id = pa.person_id
select * from #people_info
Which of course does not work for some reason.
If I query the two joined tables and select "pa.attribute_value where pa.attribute_type_id = 'someval'" I get the correct value. But the update does not work.
Of course, I can write this as three updates, but I am thinking that it will be faster to do one join and then to filter in the update clause.
Also, please keep in mind that my attributes are spread over three tables, not just the attributes table. So, this is why I have the table variable.
Any help is very welcome. Maybe I am going about this the wrong way. Performance matters. What is the most performant way to accomplish this?
Thank you very much.
Try this code for update with pivot:
update
pi
set
pi.height = pa.Height
pi.weight = pa.Weight
pi.eye_color = pa.EyeColor
from
#people_info pi
inner join
(
SELECT
person_id
,[Height] Height
,[Weight] Weight
,[EyeColor] EyeColor
FROM
(
SELECT
attribute_type_id
, attribute_value
, person_id
FROM
dbo.HR.person_attributes pa
) pa
PIVOT
(
MAX(attribute_value) FOR attribute_type_id IN ([Height],[Weight],[EyeColor])
)pvt
) pa
on
pi.person_id = pa.person_id
Maybe you want something like:
update pi
set
pi.height = paH.attribute_value,
pi.weight = paW.attribute_value,
pi.eye_color = paE.attribute_value
from
#people_info pi
inner join dbo.HR.person_attributes paH on pi.person_id = paH.person_id
and paH.attribute_type_id = 'Height'
inner join dbo.HR.person_attributes paW on pi.person_id = paW.person_id
and paW.attribute_type_id = 'Weight'
inner join dbo.HR.person_attributes paE on pi.person_id = paE.person_id
and paE.attribute_type_id = 'EyeColor'
Can anyone tell me how to write the following query in Entity Framework 4.0? "Blogs" and "Categories" are my entities. The query basically returns me the list of categories and the number of blogs that are in that category.
SELECT b.CategoryId, c.Value, Count(b.Id) AS [Count] FROM dbo.Blogs b
INNER JOIN dbo.Categories c ON b.CategoryId = c.Id
GROUP BY b.CategoryId, c.Value
Thanks in advance.
The following should work (LinqToEntities):
var categories = from c in oc.Categories
select new
{
CategoryId = c.Id,
c.Value,
Count = c.Blogs.Count()
}
This will give you a list of category ids and values and for each category id you get the number of blogs in that category.
EDIT: To give an answer to the question in your comment: this isn't possible in LinqToEntities but you can do it in Entity SQL.
var results = new ObjectQuery<DbDataRecord>(
#"SELECT y, COUNT(y)
FROM MyEntities.Blogs AS b
GROUP BY YEAR(b.CreatedDate) AS y", entities).ToList();
var nrBlogsPerYear = from r in results
select new { Year = r[0], NrBlogs = r[1] };
In the Entity SQL query, you should replace MyEntities with the name of your context.
EDIT: As I just found out through a comment by Craig, grouping by year is possible in L2E so you can write your query like this:
var nrBlogsPerYear = from b in oc.Blogs
group b by b.CreatedDate.Year into g
select new { Year = g.Key, NrBlogs = g.Count() };
If you have navigation properties in your entities, you can do that :
var cats = from c in db.Categories
let count = c.Blogs.Count()
where count > 0
select new
{
CategoryId = c.Id,
Value = c.Value,
Count = count
};
If you prefer to use an explicit join, you can do it like that :
var cats = from c in db.Categories
join b in db.Blogs on c.Id equals b.CategoryId into g
select new
{
CategoryId = c.Id,
Value = c.Value,
Count = g.Count()
};
var query = from p in B.Products
join c in B.Categories on p.CategoryID equals c.CategoryID
orderby c.CategoryName,p.ProductName
select new
{
c.CategoryName ,
p.ProductName,
p.ReorderLevel
};
GridView1.DataSource = query.ToList();
GridView1.DataBind();