I was wondering if there is a way that I could make this block code shorter:
if (rdball.Checked ==true)
{
var query = from u in context.User
join ur in context.UserRole on u.ID equals r.UserID
join r in context.Role on ur.RoleID.ToString() equals r.ID.ToString()
select new
{
u.ID,
u.Nick,
u.LastLogin,
Role = ur == null ? String.Empty : r.Name
};
}
else
{
var query = from u in context.User
join ur in context.UserRole on u.ID equals r.UserID
join r in context.Role on ur.RoleID.ToString() equals r.ID.ToString()
where sg.GroupID == Convert.ToInt32(cmbGroupSearch.SelectedValue)
select new
{
u.ID,
u.Nick,
u.LastLogin,
Role = ur == null ? String.Empty : r.Name
};
}
Without EF I normally make a string with the default query and add the last part with the where depending if the radioubutton is checked or not. Like this
if (rdball.Checked ==true)
query = query + " where sg.GroupID ==" + Convert.ToInt32(cmbGroupSearch.SelectedValue)
end if
But I dont understand how to do this with EF. And I would not like that 2000 lines of code become 3000 just for repeat querys. Thanks in advance.