0

我正在尝试将下面的 TSQL 转换为 LINQ。我显然不太擅长这一点,也不知道如何在 Linq 中加入。我的困惑不仅仅是简单的表达。在我的基本用途中,我的结果集是一个当然代表表格行的类......但是连接不会有这个一对一的比例,所以我必须创建一个自定义结果集吗?

TSQL:

SELECT 
  SimEA.*,
  SimE.*
FROM 
  dbo.SSimEmailsAdressees SimEA

JOIN dbo.SSimEmails SimE ON
SimEA.EmailID = SimE.EmailMsgID

WHERE
 SimEA.UserId = var

我最接近的:

this.GetAll<SSimEmails>() 
    .Where(e => e.SSimEmailsAdressees.Any(p => p.UserId.ToString() == usrID));

这当然会返回一个模拟被查询的单个表的对象。所以我需要帮助来添加连接,我猜我必须创建一个类似这样的返回对象?

this.GetAll<MyJoinResultsObject>......
4

2 回答 2

1

我认为你应该写这样的东西:

var q = 
        from SimEA in SSimEmailsAdressees
        where SimEA.UserId = usrID
        join SimE in SSimEmails on SimEA.EmailID equals SimE.EmailMsgID
               into joinSimE
        from SimE in joinSimE.DefaultIfEmpty() 
        select new { EmailID = SimEA.EMailID, UserId = SimEA.UserId, OtherField = SimE.OtherField }; 
于 2013-03-23T10:48:12.147 回答
0
var result = from ea in SimEA
         join e in SimE
         on ea.EmailID equals e.EmailMsgID
     where ea.UserId = userId
         select new { properties you want here or new object based on properties}
于 2013-03-23T10:52:13.947 回答