独特的 LINQ 语句 - 选择实体不存在的组

发布于 2024-10-10 22:40:54 字数 1474 浏览 4 评论 0原文

我当前有一个 LINQ 语句,它返回要显示到 Telerik RadGrid 中的 IQueryable。该语句设置为提取与输入的期间匹配的记录,并且还将“Premium”列设置为 true。然后它选择 EmployeeID & ProjectID 明显使用 GroupBy 属性。

然后,这些列与“PremiumCode”列一起显示在 RadGrid 中。 目前,我的语句可以显示满足顶级凭据(员工姓名、项目、高级代码)的所有记录,但我的最终目标是仅提取那些已经具有分配给该特定员工的项目的“PremiumCode”

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;
    }

目前它显示正常,但每条记录都会显示,而不仅仅是需要 PremiumCode 的记录。

alt text

有没有办法重新处理我的 LINQ 语句,使其仅包含需要 高级代码?

编辑:
杰伊, 我尝试修改您的解决方案以满足我的需求,但不幸的是没有成功。在定义 Premium Code 之前,不会添加 Premium 表中的记录,因此永远不会有空的“PremiumCode”。

为了更清楚地描述我的最终目标:我希望在网格中显示信息,如上图所示。显示的记录将是不同的时间记录,其布尔值“Premium”检查为 true,但在 Premium 表中没有 PremiumCode 记录。

如果检查的记录在 Premium 表中有匹配的记录(EmployeeID 和 ProjectID 匹配),那么它已经拥有 Premium 代码集,并且不需要显示在网格。

如果检查的记录在 Premium 表中没有匹配的记录(EmployeeID 和 ProjectID 不匹配),则它需要 PremiumCode 并且需要在网格中显示。

我相信这可以通过“.Any()”来实现,但我在调整语法和逻辑以使该网格正确显示时遇到了麻烦。

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.

alt text

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 matching) 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽蝶幻影 2024-10-17 22:40:54

怎么样:

 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;

更新以回应问题编辑

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;

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;
流殇 2024-10-17 22:40:54

如果高级代码是字符串,您可能需要尝试在 GroupBy 子句之前添加类似 .Where(x => string.isNullOrEmpty(x.PremiumCode)) 的内容。

If premium code is a string, you might want to try adding something like .Where(x => string.isNullOrEmpty(x.PremiumCode)) before the GroupBy clause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文