我有两个表:设计和网址。设计可以通过Guid与许多URL相关(这不是Design表的主键,因为URL也可以通过同一列与其他表相关)。
Design (table)
-------
DesignGuid
URL (table)
------
RelationalGuid
UrlString
在我的代码中,我试图确定我给出的某些信息是否需要创建新的设计记录,或者它是否与现有的设计记录相对应。该信息包括URL列表。这些URL是确定是否需要创建新记录的唯一方法。
var localUrlStrings = new List<string>() { "urlString1", "urlString2" }
因此,我需要获取一个我给出的URL列表,并编写一个查询现有设计记录及其关联URL的查询,并返回匹配网址匹配的匹配。所以,如果我的代码中有2个URL,我只需要找到那些与2个完全匹配2个URL的Designs(应该是1或0个匹配)。
谢谢, Jeromeyers
答案 0 :(得分:2)
如果我正确理解您的问题,我认为您想要一个看起来像这样的SQL查询:
SELECT d.DesignGuid
FROM Design AS d
INNER JOIN URL as u ON d.DesignGuid = u.RelationalGuid
WHERE u.UrlString IN ('urlstring1', 'urlstring2', ... ,'urlstringn')
我认为这样的事情应该有效:
var localUrlStrings = new List<string>() { "urlString1", "urlString2" }
var result =
from d in Design
from u in URL
where d.DesignGuid == u.RelationalGuid
&& localUrlStrings.Contains(u.UrlString)
select new {
d.DesignGuid,
u.UrlString
});
编辑:您正在寻找:
SELECT d.DesignGuid, count(u.URL) as numURLs
FROM Design as d,
Url as u
WHERE d.DesignGuid = u.RelationalGuid
HAVING count(u.URL) = 2
我现在无法测试它,但我相信这应该可以解决问题(语法可能有点偏差,我会在早上仔细检查):
var result =
from d in dataContext.Designs
join u in dataContext.URLs on d.DesignGuid equals u.RelationalGuid into joinTable
from j in joinTable
where localUrlStrings.Contains(j.UrlString)
group j by j.RelationalGuid into groupTable
where groupTable.Count() == localUrlStrings.Count
select groupTable.Key;
答案 1 :(得分:0)
尝试(不完全确定所需的确切结果):
var Result = from d in Design from u in URL from l in localUrlStrings
where d.DesignGuid == u.RelationalGuid and
l == u.UrlString select new { l, d.DesignGuid };