You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用IEnumerable<T>调用带params T[]参数的EF Core AddRange方法?

解决C#中IEnumerable适配params参数的问题

EF Core的DbSet<NotificationRecord>其实自带了支持IEnumerable<NotificationRecord>的AddRange重载,你完全不需要做额外的转换操作,直接传入参数就行:

public async Task AddNotificationRecordsAsync(
               IEnumerable<NotificationRecord> notificationRecords,
               CancellationToken cancellationToken = default)
{
    // 替换成你的DbContext实例和对应的DbSet名称
    _dbContext.NotificationRecords.AddRange(notificationRecords);
    await _dbContext.SaveChangesAsync(cancellationToken);
}

要是碰到某些方法只提供params T[]重载、没有IEnumerable版本的情况,你可以用ToArray()方法把IEnumerable转换成数组再传入,比如:

// 假设有个只有params重载的方法
public void ExampleMethod(params NotificationRecord[] entities) { }

// 调用时转换
ExampleMethod(notificationRecords.ToArray());

内容的提问来源于stack exchange,提问作者Shuzheng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 21:12:10