0

我有一个名为的表IssueStatuses和另一个名为 Issues 的表。问题有 aStatusIDSubStatusID,两者都来自IssueStatuses表,该表有一个附加字段,说明它是否是 a SubStatus,如下所示:

问题状态

  • 问题状态ID
  • 问题状态
  • 是子状态

我正在尝试获取SubStatuses特定问题列表的列表。在 SQL 中是:

SELECT iss.IssueStatus, COUNT(iss.IssueStatus) AS Total
FROM Issues AS Issues 
INNER JOIN Rooms r ON Issues.RoomID = r.RoomID 
INNER JOIN Locations l ON l.LocationID = r.LocationID 
INNER JOIN Customers c ON l.CustomerID = c.CustomerID 
INNER JOIN (SELECT * FROM IssueStatuses WHERE IsSubStatus = 0) ist ON Issues.IssueStatusID = ist.IssueStatusID 
INNER JOIN (SELECT * FROM IssueStatuses WHERE IsSubStatus = 1) iss ON Issues.IssueSubStatusID = iss.IssueStatusID 
WHERE c.Customer = 'ABC' 
AND l.Location = 'MySite'
GROUP BY iss.IssueStatus

但我在将其转换为 LINQ 时遇到问题。所需的输出类似于:

问题状态 | 全部的
------------------
打开 15
延迟 25
准时 8

这是我用 LINQ 尝试过的:

var query = from i in Issues
join r in Rooms on i.RoomID equals r.RoomID
join l in Locations on r.RoomID equals l.LocationID
join c in Customers on l.CustomerID equals c.CustomerID
where i.IssueStatusID == (from ist in IssueStatuses
                            where ist.IsSubStatus == false
                            select ist)
&& i.IssueSubStatusID == (from iss in IssueStatuses
                            where iss.IsSubStatus == true
                            select iss)
&& c.Custome == "ABC"
&& l.Location == "MySite"
    group i by i.IssueStatus

但我知道这是错误的,因为LINQPad会引发错误说明:

can't convert int to type Models.IssueStatus

我需要做的是用于iss.IssueStaus分组,但我无法访问它。有人可以告诉我我做错了什么吗?

4

2 回答 2

1

这个怎么样(未经测试,但我应该很接近):

var query = from i in Issues
            join r in Rooms on i.RoomID equals r.RoomID
            join l in Locations on r.LocationID equals l.LocationID
            join c in Customers on l.CustomerID equals c.CustomerID
            join ist in IssueStatuses on i.IssueStatusID equals ist.IssueStatusID
            join iss in IssueStatuses on i.IssueSubStatusID equals iss.IssueStatusID
            where !ist.IsSubStatus && iss.IsSubStatus
                && c.Customer == "ABC"
                && l.Location == "MySite"
            group i by iss.IssueStatus into g
            select new {IssueStatus = g.Key, Total = g.Count()}
于 2013-01-22T17:52:57.753 回答
0

您的两个内部from语句返回对象而不是 ID ...选择您需要的 ID,例如ist.IssueStatusID

var query = from i in Issues
join r in Rooms on i.RoomID equals r.RoomID
join l in Locations on r.RoomID equals l.LocationID
join c in Customers on l.CustomerID equals c.CustomerID
where (from ist in IssueStatuses
                            where ist.IsSubStatus == false
                            select ist.IssueStatusID).Contains(i.IssusStatusID)
&& (from iss in IssueStatuses
                            where iss.IsSubStatus == true
                            select iss.IssueStatusID).Contains(i.IssueSubStatusID)
&& c.Customer == "ABC"
&& l.Location == "MySite"
    group i by i.IssueStatus
于 2013-01-22T17:47:35.147 回答