gpt4 book ai didi

entity-framework - 我的列表应该包含什么类型?

转载 作者:行者123 更新时间:2023-12-01 06:38:02 25 4
gpt4 key购买 nike

我有这个查询

List<int> AuctionIds = 
(from a in _auctionContext.Auctions
where a.AuctionEventId == auction.AuctionEventId
select new { a.Id }).ToList();

但我收到一个编译错误
Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<int>'

AuctionIds 应该是什么类型?

编辑

AuctionIds 字段实际上在另一个类(模型类)中,所以我不能只使用 var。我不敢相信 Jon Skeet 没有回答这个问题。

最佳答案

您正在向 List<int> 添加匿名对象..
如果你按照你的方式去做......我会使用 var关键词..

var AuctionIds = 
(from a in _auctionContext.Auctions
where a.AuctionEventId == auction.AuctionEventId
select new{Id = a.Id}).ToList();

原因是我不知道匿名对象是什么类型……但编译器应该能够处理它。

编辑:

呃,关于创建一个 AuctionIDModel类(class)?
 public class AuctionIDModel
{
int Id{get;set;}
}

List<AuctionIDModel> AuctionIds =
(from a in _auctionContext.Auctions
where a.AuctionEventId == auction.AuctionEventId
select new AuctionIDModel{Id = a.Id}).ToList();

关于entity-framework - 我的列表应该包含什么类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13251313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com