I'll try to respond with an example:
See image below:
ThePeopletableisrelatedtoPhone,anditsrelationisnotmandatory,ieIcanwritePhone
withoutPeople
(Phone.PeopleIdcancontainNULL
values).FormetogetallPhone
independentofPeople
IhavetouseLEFTJOIN
.
Linq( Linq To SQL ) looks like this: / em>
using (cboEntities db = new cboEntities())
{
var result = db.Phone
.GroupJoin(db.People, ph => ph.PeopleId, pe => pe.PeopleId, (ph, pe) => new { ph, pe })
.SelectMany(s => s.pe.DefaultIfEmpty(), (ph, pe) => new
{
ph.ph.PhoneId,
ph.ph.Ddd,
ph.ph.Number,
ph.ph.PeopleId,
ph.ph.People.FirstName
});
var resultToList = result.ToList();
}
or
using (cboEntities db = new cboEntities())
{
var result = (from ph in db.Phone
join pe in db.People on ph.PeopleId equals pe.PeopleId into peph
from pe_ph in peph.DefaultIfEmpty()
select new
{
ph.PhoneId,
ph.Ddd,
ph.Number,
ph.PeopleId,
ph.People.FirstName
});
var resultToList = result.ToList();
}
SQL Generated:
SELECT
[Extent1].[PhoneId] AS [PhoneId],
[Extent1].[Ddd] AS [Ddd],
[Extent1].[Number] AS [Number],
[Extent1].[PeopleId] AS [PeopleId],
[Extent2].[FirstName] AS [FirstName]
FROM [dbo].[Phone] AS [Extent1]
LEFT OUTER JOIN [dbo].[People] AS [Extent2] ON [Extent1].[PeopleId] = [Extent2].[PeopleId]
Result:
Notethat%withand5,doesnothavePhoneId
,thenallrecordsinthe%People
Foryourproblem,trytoseetheresult.
DateTimedata=DateTime.Parse("10/06/2014");
String local = null;
var result = dataClass.vwPLANODINs
.Where(x =>
!dataClass.LANCADA.Where(la => la.Data == data).Select(g => g.Subord).Contains(x.Codplano) &&
x.Sitio == local
)
.Select(s => new
{
s.Codplano,
s.Secao,
s.Setor,
s.SubSetor,
s.Contato,
Situacao = "NÃO LANÇADA"
});
var resultToList = result.ToList();
Explanation:
When I think of doing a Lambda Expression , first thought about SQL that the Entity Framework should generate. As you said you want to know "Teams that did not have document released on a certain date" and looked so much at their SQL I thought it was not going to do what you wanted. I then decided to create a SQL like this:
SELECT
[Extent1].[Codplano] AS [Codplano],
[Extent1].[Secao] AS [Secao],
[Extent1].[Setor] AS [Setor],
[Extent1].[SubSetor] AS [SubSetor],
[Extent1].[Contato] AS [Contato]
FROM [dbo].[vwPLANODINs] AS [Extent1]
WHERE ( NOT EXISTS (SELECT 1 AS [C1] FROM [dbo].[LANCADA] AS [Extent2]
WHERE ([Extent2].[Data] = "2014-06-10") AND ([Extent2].[Subord] = [Extent1].[Codplano]))
) AND [Extent1].[Sitio] = 7341)
Using NOT EXITS and with the Search fronts it became more efficient in that way, and with that I became Linq To SQL as already posted just above.
Tip: Always think about SQL to then think about Entity Framework .
Obs: I have an example here of how to test the SQL generated by the Entity Framework.