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;
Related
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();
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 4 years ago.
Improve this question
I have this code
var value = (from dc in _context.ContractDetails
where dc.EmployeeID == id
select dc.Amount);
return value;
}
is it acceptable to do Value.Sum();
You want to return the sum it looks like. Instead of having query be a decimal, just let it be what it wants (var, it's really IEnumerable<decimal>). Then you can return an aggregate on that. Sum for example
var query = from emp in Employees
join cd in ContractDetails
on emp.EmployeeID equals cd.EmployeeID
where cd.EmployeeID == id
select cd.Amount;
return query.Sum();
If this is all it does, then I also feel like you don't need to join at all, and it would be simpler to do
var query = from cd in ContractDetails
where cd.EmployeeID == id
select cd.Amount;
return query.Sum();
... unless you were using the join to test for the existence of an employee in the Employee table as a condition.
Your linq statement results in an IQueryable<Amount>, you would need to take that result and call Sum() on it to get the result you're seeking.
First, isn't there a navigation property you can use (i.e. Employee.ContracteDetails) instead of manually joining the two sets? For example,
var sum = _context.Employee
.Where( e => e.Id == id )
.Select( e => e.ContractDetails.Sum( cd => cd.Amount ) )
.SingleOrDefault();
Second, you're not using any information you need from Employee, even your where clause references ContractDetails alone; why start your query there? Work with _context.ContractDetails instead:
var sum = _context.ContractDetails
.Where( cd => cd.EmployeeId == id )
.Sum( cd => cd.Amount );
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 6 years ago.
Improve this question
I am trying to retrieve role info as below:
var allRoles = (from roles in context.aspnet_Roles
select new { roles.RoleId, roles.RoleName }).ToList();
I want to store these role data to a specific class type or LIST<string> not to object of type var like allRoles.
You are currently selecting a list of anonymous type. What you seem to want is just the name and as such you should select just the name as a result instead of projecting a new object.
var allRoles = (from roles in context.aspnet_Roles
select roles.RoleName).ToList();
To change the things stored in the list, you change the bit after the select keyword. For example:
var allRoles = (from roles in context.aspnet_Roles
select "ID=" + roles.RoleId + ";Name=" + roles.RoleName).ToList();
The specific expression following the keyword will depend on the string format you want for the list's elements.
By the way, this code does not have any lambda expressions in it.
What I understand from these details is that there are a few columns in "context.aspnet_Roles" but, you only want to use RoleId and RoleName.
Here is what I would do :
List<MagicClass> allRoles= new List<MagicClass>();
allRoles= (from roles in context.aspnet_Roles
select new MagicClass
{
RoleId = roles.RoleId,
RoleName = roles.RoleName
}).ToList();
internal class MagicClass
{
private string RoleId {get;set;}
private string RoleName {get;set;}
};
Hope this helps.
I would suggest if you want to store role name only use this
var allRoles = (from roles in context.aspnet_Roles
select new { roles.RoleName }).ToList();
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
};
This question already has answers here:
LINQ Left Join And Right Join
(4 answers)
Closed 9 years ago.
I'm trying to convert this query (Already working)
select [User].ID AS ID_USER
from [User]
right join Building on Building.ID = 4 AND
Building.ID_USER_RESPONSIBLE <> [User].ID
where [User].ID_MANAGER = 1
To Linq to SQL, but I don't know what I'm doing wrong. Look at my trying
IList<User> lstUser = (from building in db.GetTable<Building>()
join user in db.GetTable<User>()
on new
{
ID_BUILDING = building.ID,
ID_USER = building.ID_USER_RESPONSIBLE
}
equals new
{
ID_BUILDING = 4,
ID_USER != user.ID
} into grpUser
from grp in grpUser.DefaultIfEmpty()
where building.ID_MANAGER = 1
select new
{
ID_USER =
});
I need to return all Users plus the Users which aren't responsible to any Building;
EDIT
Any of solution posted here worked.
I've resolved just doing this:
var lstUser = (from building in db.GetTable<Building>()
join user in db.GetTable<User>()
on building.ID equals 4
where user.ID_MANAGER == 1 &&
building.ID_USER_RESPONSIBLE != user.ID
select new
{
ID_USER = user.ID,
NAME = user.NAME
}).ToList();
See if the following works:
IList<User> lstUser = (from building in db.GetTable<Building>()
join user in db.GetTable<User>()
on new
{
ID_BUILDING = building.ID,
ID_USER = building.ID_USER_RESPONSIBLE,
building.ID_MANAGER
}
equals new
{
ID_BUILDING = 4,
ID_USER = user.ID,
1
} into grpUser
from grp in grpUser.DefaultIfEmpty()
select new
{
ID_USER = grp.ID
});
That being said, there seems to be a gap in your requirement, "I need to return all Users plus the Users which aren't responsible to any Building;" I may be mis-reading your statement but isn't the list of users that aren't responsible to a building a subset of the larger "All Users" set. As a result, you would always just get All Users regardless of their building membership.