C# 如何创建一个linq lambda连接查询,即使结果为空,也能提取结果?

C# 如何创建一个linq lambda连接查询,即使结果为空,也能提取结果?,c#,mysql,linq,lambda,C#,Mysql,Linq,Lambda,我有一个加入查询,它会将一个人购物车中的所有学校课程和产品都拉入其中: //this pulls all items the user purchased var poop = Context.Query<Cart>().Where(x => x.UserId == currentUserId && x.Status == "Archived") .Select( p => new { p.I

我有一个加入查询,它会将一个人购物车中的所有学校课程和产品都拉入其中:

//this pulls all items the user purchased
var poop = Context.Query<Cart>().Where(x => x.UserId == currentUserId && x.Status == "Archived")
    .Select(
        p => new
        {
            p.ItemId,
            p.TypeId,
            p.PurchaseDate
        })
//This get the media type name of the cart items
    .Join(
        Context.Query<MediaType>(),
        t => new {t.TypeId},
        m => new {TypeId = m.Id},
        (t, m) => new
        {
            t.ItemId,
            t.TypeId,
            t.PurchaseDate,
            m.TypeName
        }).OrderBy(d => d.PurchaseDate)
//Now i need specifics of the items like name, sku, etc. StartDate will be null for items that are products, but contains DateTime for items that are programs.
    .Join(
        Context.Query<ProgramProductView>(),
        e => new {e.ItemId, e.TypeId},
        prog => new {ItemId = prog.Id, prog.TypeId},
        (e, prog) => new
        {
            e.ItemId,
            e.TypeId,
            e.PurchaseDate,
            e.TypeName,
            prog.FullName,
            prog.StartDate,
            prog.Sku,
            prog.Closed
        }).OrderBy(d => d.PurchaseDate);
//这将提取用户购买的所有项目
var poop=Context.Query().Where(x=>x.UserId==currentUserId&&x.Status==“已存档”)
.选择(
p=>新的
{
p、 ItemId,
p、 TypeId,
p、 购买日期
})
//这将获取购物车项目的媒体类型名称
.加入(
Context.Query(),
t=>new{t.TypeId},
m=>new{TypeId=m.Id},
(t,m)=>新
{
t、 ItemId,
t、 TypeId,
t、 购买日期,
m、 字体名
}).OrderBy(d=>d.PurchaseDate)
//现在我需要项目的详细信息,如名称、sku等。对于产品项目,StartDate将为空,但对于程序项目,StartDate包含DateTime。
.加入(
Context.Query(),
e=>new{e.ItemId,e.TypeId},
prog=>new{ItemId=prog.Id,prog.TypeId},
(e,prog)=>新
{
e、 ItemId,
e、 TypeId,
e、 购买日期,
e、 字体名,
prog.FullName,
程序开始日期,
项目Sku,
程序关闭
}).OrderBy(d=>d.PurchaseDate);
这就是它崩溃的地方,因为prog.StartDate对于产品是空的。我得到的SQL是不可用的错误


有没有办法让join允许空字段?我之所以使用lambda,是因为它更易于阅读和清理。

好吧,您只需要在上一次加入时为您的匿名类使用
Nullable
属性:

.Join(
    Context.Query<ProgramProductView>(),
    e => new {e.ItemId, e.TypeId},
    prog => new {ItemId = prog.Id, prog.TypeId},
    (e, prog) =>
        new
        {
            ...
            (DateTime?)prog.StartDate,
            ...
         }).OrderBy(d => d.PurchaseDate);
.Join(
Context.Query(),
e=>new{e.ItemId,e.TypeId},
prog=>new{ItemId=prog.Id,prog.TypeId},
(e,prog)=>
新的
{
...
(日期时间?)项目开始日期,
...
}).OrderBy(d=>d.PurchaseDate);

希望能有所帮助。

试试
(DateTime?)prog.StartDate
*facepalm。。。。哇,这很简单。谢谢你能回答这个吗?把这个写进答案里。