LINQ& Enum with Left Join

时间:2017-08-25 16:51:47

标签: c# linq

我有一个枚举

public enum Status { New, InProgress, Processed, InComplete};

我有以下查询要查询,根据状态给我列表计数。但是现在我只是在它存在的情况下才会得到它。因此,如果Processed count为零,我将不会获得任何值。在SQL中我会做一个左连接但不太确定如何在LINQ中这样做。也不确定如何使用枚举类型进行LEFT连接。

这是我的查询

var results = DbContext.Orders
                       .Where(i => i.Id== Id)
                       .GroupBy(row => new { row.Status})
                       .Select(g => new Stats()
                       {
                           Status = g.Key.Status,
                           Count = g.Count()
                       }).ToList();

所以在sql中我会做这样的事情。假设我有一个状态表。

SELECT status_id, s.name, count(status_id) count
FROM order o LEFT JOIN status s ON (o.status_id = s.status_id)
WHERE o.id = 1
GROUP BY status_id, s.name

我假设我会得到这样的结果 新11 InProgress 5 已处理0 InComplete 0

在我的LINQ查询中,我不会获得New Processed或InComplete。

1 个答案:

答案 0 :(得分:5)

一种方法是在数据库中运行select以获取当前结果,然后通过加入内存中的枚举来填充空白,如下所示:

File "C:\Users\User-PC\Desktop\check.py", line 3, in <module>
response = 
urllib.urlretrieve('www.domain.com\\folder\\folder\\52sd1399sc2emaple-story-
vthree-f.swf','file.swf')
File "C:\Python27\lib\urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "C:\Python27\lib\urllib.py", line 245, in retrieve
fp = self.open(url, data)
File "C:\Python27\lib\urllib.py", line 213, in open
return getattr(self, name)(url)
File "C:\Python27\lib\urllib.py", line 469, in open_file
return self.open_local_file(url)
File "C:\Python27\lib\urllib.py", line 483, in open_local_file
raise IOError(e.errno, e.strerror, e.filename)
IOError: [Errno 2] The system cannot find the path specified: 
'www.domain.com\\folder\\folder\\52sd1399sc2emaple-story-vthree-f.swf'

由于从RDBMS传输到内存的数据量没有变化,因此加入内存不会对此操作的总体成本带来重大变化。