Convert SQL query into linq C# [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Convert SQL query to LINQ C#
SELECT ts.TeacherId, count(Distinct ts.SubjectId) as Subjects
from dbo.TeacherSubjects ts
GROUP BY ts.TeacherId
HAVING ts.TeacherId = 2352
here is my LINQ query
var SubjectsGroup = db.TeacherSubjects.Where(p => p.TeacherId == 2352).Distinct().GroupBy(x => x.TeacherId);

var teacherId = ; // your teacherId here
var result = new {
TeacherId = teacherId,
Subjects = db.TeacherSubjects.Where(ts => ts.TeacherId == teacherId).Select(x => x.SubjectId).Distinct().Count()
};
Or you can group by SubjectId:
var teacherId = ; // put your teacherId here
var result =
(from ts in TeacherSubjects.Where(x => x.TeacherId == teacherId)
group ts by ts.SubjectId into gr
select new
{
TeacherId = teacherId,
Subjects = gr.Count()
}).ToList();

Related

ASP.NET using Entity Framework display data to drop down list with distinct [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Please I need to implement distinct in a.CompanyName but it doesn't work. What do I need to do ?
try
{
var listCompany = company.CompanyList();
companyList1.DataSource = listCompany.Select(a => new { a.CompanyName, a.CompanyId }).Distinct();
companyList1.DataTextField = "CompanyName";
companyList1.DataValueField = "CompanyId";
companyList1.DataBind();
}
catch (Exception)
{
throw;
}
You can use with .GroupBy()
Groups the elements of a sequence.
companyList1.DataSource = listCompany
.GroupBy(x => x.CompanyName)
.Select(a => new
{ CompanyName = a.First().CompanyName, CompanyId = a.First().CompanyId });
Above query will group your result by company name and then will select CompanyName and CompanyId of first record
Try out online

Get NOT IN on a one to many relationship using LINQ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a table LookUpRole as so
LookUpRoleId | RoleName
another table called UserAccounts as so
UserAccountId | FirstName | LastName
another table called UserRole as so
UserRoleId | LookUpRoleId | UserAccountId
This is what I have come up with:
using (var db = new AMSEntities())
{
var roleList = (from lur in db.LookUpRoles
join ur in db.UserRoles on lur.LookUpRoleId equals ur.LookupRoleId
join ua in db.UserAccounts on ur.UserId equals ua.UserAccountId
where ua.UserAccountId == accountId
select new UserRole()
{
LookupRoleId = lur.LookUpRoleId,
UserId = ua.UserAccountId
}).ToList();
var lackingRoles = db.UserRoles.Where(x => !roleList.Contains(x.LookupRoleId)).ToList();
}
A UserAccountId may have multiple LookUpRoleId. I just want to get the list of RoleName or LookUpRoleId that is not related to the UserAccountId yet. For example; UserAccountId of 1 has LookUpRoleId of 1 and 2. I need to get the RoleName of LookUpRoleId 3 and 4 using LINQ. Thank you.
Use a left join from the roles to the user-roles. Then query those that are null:
var result = from role in db.LookUpRoles
join ur in db.UserRole
on { role.LookUpRoleId, accountId } equals new {ur.LookUpRoleId, ur.UserAccountId } into j
from ur in j.DefaultIfEmpty()
where ur == null
select role.RoleName;
You can also do:
var result = from role in db.LookUpRoles
where !db.UserRole.Any(ur => ur.UserAccountId == accountId && ur.LookUpRoleId == role.LookUpRoleId)
select role.Name;

Query with case statement from SQL Server to Linq query c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have the following query in sql server
select COUNT(mc_owner) as nbr ,
case mc_owner when 'Element1' then 'Element1' else 'others' end Owner
from [dbo].[full]
where (date_reception > '01-01-2015')
group by (CASE mc_owner WHEN 'Element1' THEN 'Element1' ELSE 'others' END)
order by nbr desc
I need to convert it to entityframework linq query
Try this,
var result = fulltableData.Where(x=>x.date_reception > '01-01-2015')
.GroupBy(x=> x.mc_owner.Equals("Element1")?"Element1":"others")
.Select(x=> new{ nbr = x.Count(), Owner = x.Key})
.OrderByDescending(x=>x.nbr);
I understand you want to select the number of occurrences of a certain mc_owner compared to all others. The following LINQ query should produce the same result:
full.Where(f => f.date_reception > new DateTime(2015, 1, 1))
.GroupBy(f => f.mc_owner == "Element1" ? f.mc_owner : "others")
.OrderByDescending(group => group.Count())
.Select(group => new { Owner = group.key, nbr = group.Count());

Linq to Sql Search through Tags [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Ok so I have these tables:
Video
Id
Title
VideoTag
Id
VideoId
TagId
Tag
Id
Name
I need a LINQ to SQL query to perform a search on the Videos, either by the title or by the tags.
For the moment all I have is this for searching by the title:
IQueryable<Video> videos =
from v in ctx.Videos
where v.Title.Contains(SearchString)
select
new Video
{
Id = v.Id,
Title = v.Title
};
Try this:
from v in ctx.Videos
join vt in ctx.VideoTags on v.Id equals vt.VideoId
join t in ctx.Tag on vt.TagId equals t.Id
where v.Title.Contains(SearchString) || t.Name.Contains(SearchString)
select
new Video
{
Id = v.Id,
Title = v.Title
};

How to re-write this linq from query syntax to method syntax? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
As in title, how to rewrite this linq query into method syntax?
(from row in recordsEpP
where (!string.IsNullOrEmpty(row.Column20)
&& !string.IsNullOrEmpty(row.Column14)
&& string.IsNullOrEmpty(row.Column8))
select new ReportDto{
Status = "P",
ContractNumber = row.ContractNumber,
Count = 1
}).ToList();
You could try something like this:
recordsEpP.Where(row=> !string.IsNullOrEmpty(row.Column20) &&
!string.IsNullOrEmpty(row.Column14) &&
string.IsNullOrEmpty(row.Column8)
).Select(row => new ReportDto{
Status = "P",
ContractNumber = row.ContractNumber,
Count = 1
}).ToList();

Categories

Resources