坚持LINQ to SQL Query

时间:2010-07-07 15:34:46

标签: c# .net linq-to-sql

我似乎无法将其转换为LINQ:

    select stuff
    FROM history INNER JOIN profiles ON history.username = profiles.username 
    LEFT OUTER JOIN files ON history.fileid = files.filename 
    LEFT OUTER JOIN streams ON streams.identifier = history.fileid
    LEFT OUTER JOIN galleries ON galleries.identifier = history.fileid, subscribers 
    WHERE stuff

我很难跟踪左外连接的示例。他们没有解释最新情况,只是在他们的假设情况下该怎么做。任何帮助/解释都将非常感激。

这是我开始的方式,最后一行(来自JoinedFiles.DefaultIfEmpty()中的文件)不允许我编译。 “调用'SelectMany'中的类型推断失败了。为什么这么复杂? =(

    var x = from h in db.Histories
    join prof in db.profiles on h.username equals prof.username
    join files in db.NeebitFiles on h.fileid equals files.filename into JoinedFiles
    from files in JoinedFiles.DefaultIfEmpty()
    ...?

2 个答案:

答案 0 :(得分:1)

获取Linqer(www.sqltolinq.com)的副本。它几乎可以将任何T-SQL语句转换为Linq。这不是免费的,但并不昂贵。你有30天的试用期。

答案 1 :(得分:1)

是的,我也没有多少运气。对于加入我做

(from a in db.A
where ...
select new 
{
  a.whatever,
  (from b in db.B where B.AID = A.AID select b.value).First(), //first for inner join
(from c in db.C where C.AID = A.AID select c.value).FirstOrDefault() //firstordefault for outer joins
}