Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ延迟加载和创建新对象_C#_Entity Framework_Linq - Fatal编程技术网

C# LINQ延迟加载和创建新对象

C# LINQ延迟加载和创建新对象,c#,entity-framework,linq,C#,Entity Framework,Linq,我有几节课 public class Album { public int AlbumId { get; set; } public string Title { get; set; } public Genre genre { get; set; } public virtual Artist artist { get; set; } } public class Artist { public int ArtistId { get; set; }

我有几节课

public class Album
{
    public int AlbumId { get; set; }
    public string Title { get; set; }
    public Genre genre { get; set; }
    public virtual Artist artist { get; set; }
}

public class Artist
{
    public int ArtistId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Album> albums { get; set; }
}

public class ArtistResponse
{
    public int ArtistId { get; set; }
    public string Name { get; set; }
}

public class AlbumResponse
{
    public int AlbumId { get; set; }
    public string Title { get; set; }
    public Genre genre { get; set; }
    public ArtistResponse artist { get; set; }
}
所以我通过“Include()”包含“artist”,然后创建新的AlbumResponse,在这里我创建了新的ArtisResponse。不幸的是,它不起作用。它抛出一个InvalidOperation异常。“i.artist.ArtistId”是一个空值,我不知道为什么,没有ArtistId初始化,一切正常,只有“Name=i.artist.Name”可以完美地工作

如何将ArtistId附加到“AlbumResponse”中的“artist”? 异常:“转换为值类型'System.Int32'失败,因为具体化的值为null。结果类型的泛型参数或查询必须使用可为null的类型”
Album和Artist类位于数据库中,AlbumId和ArtistId不可为空。ArtistResponse和AlbumResponse只是用于添加或从数据库获取信息的“额外”类,所以“*Response”中的ArtistId和AlbumId如果
artist
导航属性的
ArtistId
列是
null
ArtistResponse
类型的
ArtistId
属性的类型是
int
,则应检查是否返回了值:

return ctx.Albums.Include(i => i.artist).Select(i => new AlbumResponse
{
    AlbumId = i.AlbumId,
    artist = new ArtistResponse { Name = (i.artist != null) ? i.artist.Name : string.Empty, ArtistId = (i.artist != null && i.artist.ArtistId.HasValue) ? artist.ArtistId.Value : 0 },
    genre = i.genre,
    Title = i.Title
}).ToList();
这显然只是一个猜测,因为您还没有发布类型的定义,也没有收到实际的错误消息

编辑:

如果将
ArtistResponse
类型的
ArtistId
属性的类型更改为
null
int?
):

公共类艺术响应
{
公共可空ArtistId{get;set;}
公共字符串名称{get;set;}
}

如果
artist
导航属性的
ArtistId
列是
可空的
,并且
ArtistResponse
类型的
ArtistId
属性的类型是
int
您应该检查是否有返回的值:

return ctx.Albums.Include(i => i.artist).Select(i => new AlbumResponse
{
    AlbumId = i.AlbumId,
    artist = new ArtistResponse { Name = (i.artist != null) ? i.artist.Name : string.Empty, ArtistId = (i.artist != null && i.artist.ArtistId.HasValue) ? artist.ArtistId.Value : 0 },
    genre = i.genre,
    Title = i.Title
}).ToList();
这显然只是一个猜测,因为您还没有发布类型的定义,也没有收到实际的错误消息

编辑:

如果将
ArtistResponse
类型的
ArtistId
属性的类型更改为
null
int?
):

公共类艺术响应
{
公共可空ArtistId{get;set;}
公共字符串名称{get;set;}
}
返回ctx.Albums.Include(i=>i.artist)。选择(i=>newalbumsresponse)
{
AlbumId=i.AlbumId,
Artister=new ArtistResponse{Name=i.artist.Name,ArtistId=i.artist.ArtistId??0(或Guid.NewGuid()
返回ctx.Albums.Include(i=>i.artist)。选择(i=>new AlbumResponse
{
AlbumId=i.AlbumId,

艺人=新艺人响应{Name=i.artist.Name,ArtistId=i.artist.ArtistId??0(或Guid.NewGuid()你可能,我不是说你没有完全的知识,有一个不完整的投影。实体对于数据来说有点奇怪,因为它在成为正式对象之前是无法在大门外使用的。例如:你的where、select和其他扩展语法还不能工作,因为对象还没有实现

您可能希望尝试以下简单的方法:

return ctx.Albums.Include(i => i.artist)
.ToList()


否则投影可能无法实现,因为它的导航属性为“artist”。ToList()是穷人快速实现投影的方法,本质上说:“.NET现在作为合法的对象集合是可以的”。你也可以使用我相信的其他扩展,如“First”。这只是一个猜测,但我知道我已经与EF合作了一段时间,而“realization”在执行加载事件、数学和其他事情时往往是罪魁祸首。也可以像其他人所说的那样,你只是得到了一个对导航的空引用,它就爆炸了。我用一个?.conditional运算符标识一些伪代码(如果父项也为null,则基本上通过链检查是否为null,而无需吹扫)然后一个合并的空操作符“??”来表示这个东西是否为空,并指定一个默认值。

你可能,我不是说你没有完全的知识,有一个不完整的投影。实体对于数据来说有点奇怪,因为它在成为正式对象之前是无法在大门外使用的。例如:你的where,select和其他扩展syn税收还不起作用,因为目标还没有实现

您可能希望尝试以下简单的方法:

return ctx.Albums.Include(i => i.artist)
.ToList()


否则投影可能无法实现,因为它的导航属性为“artist”。ToList()是穷人快速实现投影的方法,本质上说:“.NET现在作为合法的对象集合是可以的”。你也可以使用我相信的其他扩展,如“First”。这只是一个猜测,但我知道我已经与EF合作了一段时间,而“realization”在执行加载事件、数学和其他事情时往往是罪魁祸首。也可以像其他人所说的那样,你只是得到了一个对导航的空引用,它就爆炸了。我用一个?条件运算符标识一些伪代码(基本上是通过链检查空值,如果父项也是空的,则不吹空),然后一个合并空运算符“?”来表示该值是否为空,并指定一个默认值。

这不起作用,它显示“HasValue”错误,“int不包含HasValue的定义…”。根据我的编辑,尝试使ArtistResponse类的ArtistId属性为空。它现在可以工作:)但我真的不明白为什么..你能解释一下吗?你不能将int的值设置为null,因为int是不可为null的值类型。但是可以将null设置为null或int值。非常感谢。这不起作用,它显示了“HasValue”上的错误,“int不包含HasValue的定义…”。根据我的编辑,尝试使ArtistResponse类的ArtistId属性为空。它现在可以工作:)但我真的不明白为什么。。请解释一下,好吗?不能将int的值设置为nu
return ctx.Albums.Include(i => i.artist)
.Select(i => new AlbumResponse
            {
                AlbumId = i.AlbumId,
                artist = new ArtistResponse {  Name= i?.artist?.Name ?? String.Empty, ArtistId=i?.artist?.ArtistId ?? 0},
                genre = i.genre,
                Title = i.Title
            }).ToList();