linq投影关系属性[英] LINQ Projecting relation properties with null

本文是小编为大家收集整理的关于linq投影关系属性的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想从表中选择项目,这些项目具有关系属性,如果它们可以无效(即左JOIN),我可以投射它们吗?如果没有,我怎么能解决这个问题?

class MyProducer
{
  ....
}    

Model model = new Model();
var q = 
    model.Products
    .Select(
      p => 
        new 
        { 
            id = p.Id, 
            producer = p.Producer != null ? new MyProducer { id = p.Producer.Id } : null 
        });

var r = q.ToArray();

当我执行此代码时,我有异常

无法创建" myproducer"类型的零常数值.仅有的 实体类型,枚举类型或原始类型在 此上下文.

推荐答案

为什么不使用左JOIN?

using(var model = new Model())
{
    var q = 
    from product in model.Products
    join producer in model.Producers.DefaultIfEmpty()
    on product.ProducerId equals producer.Id
    select new
    {
        Id = product.Id,
        Producer = producer != null ? new MyProducer{ Id = producer.Id} : null
    }
}

其他推荐答案

您需要将null投射到Struct:

Model model = new Model(); 
var q = model.Products.Select(p => new { id = p.Id, producer =  
    p.Producer != null ? new Struct { id = p.Producer.Id } : (Struct)null }); 

其他推荐答案

不确定这是否是处理可能无效关系投影的唯一方法,但这就是我过去做到的.您可以创建新对象,然后在设置属性时适当检查null.该解决方案的唯一问题是您现在必须检查"生产者"的" ID"属性是否为" 0"才能知道该关系是否为无效.如果有一种更好的处理方法,如果有人将其发布在这里.

var q = 
    model.Products
    .Select(p => new 
        { 
            id = p.Id, 
            producer = new MyProducer 
            { 
               id = p.Producer == null ? 0 : p.Producer.Id
            }
        }
     );

var r = q.ToArray();

本文地址:https://www.itbaoku.cn/post/1557120.html

问题描述

I want to select items from a table, these items have relation properties, can I projecting them if they could be nullable (ie like left join)? And if not how I can workaround this?

class MyProducer
{
  ....
}    

Model model = new Model();
var q = 
    model.Products
    .Select(
      p => 
        new 
        { 
            id = p.Id, 
            producer = p.Producer != null ? new MyProducer { id = p.Producer.Id } : null 
        });

var r = q.ToArray();

When I execute this code I have exception

Unable to create a null constant value of type 'MyProducer'. Only entity types, enumeration types or primitive types are supported in this context.

推荐答案

Why don't you use left join?

using(var model = new Model())
{
    var q = 
    from product in model.Products
    join producer in model.Producers.DefaultIfEmpty()
    on product.ProducerId equals producer.Id
    select new
    {
        Id = product.Id,
        Producer = producer != null ? new MyProducer{ Id = producer.Id} : null
    }
}

其他推荐答案

You need to cast your null to Struct:

Model model = new Model(); 
var q = model.Products.Select(p => new { id = p.Id, producer =  
    p.Producer != null ? new Struct { id = p.Producer.Id } : (Struct)null }); 

其他推荐答案

Not sure if this is the only way to handle the projection of a possibly null relationship but this is how I have done it in the past. You create your new object and then when setting your properties check for null appropriately. The only problem with this solution is that you now have to check if the 'id' property of 'producer' is '0' to know if the relationship was null. If there is a better way of handling this I would love it if someone would post it here.

var q = 
    model.Products
    .Select(p => new 
        { 
            id = p.Id, 
            producer = new MyProducer 
            { 
               id = p.Producer == null ? 0 : p.Producer.Id
            }
        }
     );

var r = q.ToArray();