如何从 Sql 查询编写 Linq 查询

How to write Linq query from Sql query

任何人都可以指导我如何为以下 SQL 查询编写 LINQ

DECLARE @now DATETIME = dbo.GetInstanceDate(NULL);          
select * 
FROM [dbo].[creatives] AS [t0]
INNER JOIN [dbo].[contracts] AS [t1] ON [t0].[campaign_id] = [t1].[campaign_id]
LEFT OUTER JOIN [dbo].[vouchers] AS [t2] ON ([t1].[contract_id] = ([t2].[contract_id])) AND t0.creative_id = t2.creative_id
    AND ([t2].[contract_id] = 29980) 
    AND (NOT ([t2].[removed] = 1)) 
    AND ([t2].[active] = 1) 
    AND ((NOT ([t2].[start_date] IS NOT NULL)) OR (([t2].[start_date]) <= @now)) AND ((NOT ([t2].[expiration_date] IS NOT NULL)) OR (([t2].[expiration_date]) > @now)) 
    AND (NOT ([t2].[creative_id] IS NOT NULL))
where [t1].contract_id = 29980

如果您正在寻找 how to convert LEFT JOIN with complex filter 有一个简单的方法:

var query = 
   from t in context.Table
   from o in context.OtherTable
      .Where(o => o.id == t.Id && (o.Some == t.Some || o.Another == t.Another))
      .DefaultIfEmpty()
   select new { t, o };