Distinct LINQ Statement - Select Group where Entity doesn't exist
I currently have a LINQ statement that returns an IQueryable to be displayed into a Telerik RadGrid. This statement is set to pull Records that match the Period inputted, and also have the "Premium" Column set to true. It then selects the EmployeeID & ProjectID distinctly using the GroupBy property.
These columns are then displayed in the RadGrid, along with a "PremiumCode" column. Currently my statement works to display ALL of the records that meet the top credentials (Employee Name, Project, Premium Code), but my end Goal is to pull only those Records which DONT already have a "PremiumCode" assigned to the Project for that particular Employee.
public static IQueryable GetEmptyPremiums(string Period)
{
DataContext Data = new DataContext();
var PR = (from c in Data.System_Times
where c.Period == Period && c.Premium == true
orderby c.System_Employee.LName
select c).GroupBy(s => s.EmployeeID & s.ProjectID).Select(x => x.FirstOrDefault());
return PR;
}
Currently it is displaying properly, but every record is being displayed, not just the ones that require a PremiumCode.
Is there a way to re-work my LINQ statement to only include the records that need a PremiumCode?
EDIT:
Jay, I have tried to modify your solution to fit my needs, but unfortunately with no success. Records in the Premium table are not added until a Premium Code is defined, therefore there will never be a null "PremiumCode".To describe my end-goal a tad more clearly: I am looking to show the information in a grid like in the image above. The records shown will be the distinct Time records that have the bool value "Premium" checked as true but don't have a PremiumCode record in the Premium Table.
If the checked record has a matching record in the Premium table (EmployeeID, and ProjectID matching) then it already possesses a Premium Code set and will not need to be displayed in the Grid.
If the checked record has no matching record in the Premium table (EmployeeID, and ProjectID not matchin开发者_C百科g) then it requires a PremiumCode and will need to be displayed in the Grid.
I believe this can be achieved with ".Any()" but I am having troubles aligning my Syntax and Logic to make this Grid display properly.
How about:
DataContext Data = new DataContext();
var projectsWithoutPremium = Data.Premiums.Where(p => p.PremiumCode == null)
.Select(p => p.ProjectId);
var PR = (from c in Data.System_Times
where c.Period == Period && c.Premium == true
&& projectsWithoutPremium.Contains(c.ProjectId)
orderby c.System_Employee.LName
select c).GroupBy(s => s.EmployeeID & s.ProjectID).Select(x => x.FirstOrDefault());
return PR;
update in response to question edit
DataContext Data = new DataContext();
var PR = (from c in Data.System_Times
where c.Period == Period && c.Premium == true
&& !Data.Premiums.Any(p => p.ProjectID == c.ProjectID && p.EmployeeID == c.ProjectID)
orderby c.System_Employee.LName select c)
.GroupBy(s => s.EmployeeID & s.ProjectID)
.Select(x => x.FirstOrDefault());
return PR;
If premium code is a string, you might want to try adding something like .Where(x => string.isNullOrEmpty(x.PremiumCode))
before the GroupBy clause.
精彩评论