gpt4 book ai didi

c# - 与 EF Core 联合返回无法转换集合操作,因为两个操作数具有不同的 'Include' 操作

转载 作者:行者123 更新时间:2023-12-05 01:07:55 26 4
gpt4 key购买 nike

在带有 EF Core 5 的 .NET Core 5 WebAPI 项目中,我试图对 LINQ 查询进行联合,但我总是收到“无法翻译”错误。我试图连接的两个实体是相同的,并且字段定义的顺序也相同,所以我无法理解问题是什么以及为什么它不能转换为 SQL UNION:

IQueryable <MonthlyAggregatedPrice> monthlyAggregatedPrices = 
(from map in db.MonthlyAggregatedPrices
where map.Adm0Code == adm0Code
orderby map.CommodityPriceDate descending
select map).Union(
from f in db.ST_PewiPriceForecasts
join cm in db.Commodities on f.CommodityID equals cm.CommodityID
join m in db.Markets on f.MarketID equals m.MarketId
join u in db.CommodityUnits on f.CommodityUnitID equals u.CommodityUnitID
join pt in db.PriceTypes on f.PriceTypeID equals pt.PriceTypeID
join cu in db.Currencies on f.CurrencyID equals cu.CurrencyID
where f.Adm0Code == adm0Code
select new MonthlyAggregatedPrice
{
CommodityId = f.CommodityID,
MarketId = f.MarketID,
PriceTypeId = f.PriceTypeID,
CommodityUnitId = f.CommodityUnitID,
CurrencyId = f.CurrencyID,
CommodityName = cm.CommodityName,
MarketName = m.MarketName,
PriceTypeName = pt.PriceTypeName,
CommodityUnitName = u.CommodityUnitName,
CurrencyName = cu.CurrencyName,
Adm0Code = adm0Code,
CountryISO3 = countryInfo.Iso3Alpha3,
CountryName = countryInfo.Name,
CommodityPrice = 0,
OriginalFrequency = "monthly",
CommodityPriceSourceName = "",
CommodityPriceObservations = null,
CommodityDateMonth = f.PriceForecastMonth,
CommodityDateYear = f.PriceForecastYear,
CommodityPriceDate= f.PriceDate,
CommodityPriceFlag = "forecast"
});

MonthlyAggregatedPrice 实体是:

public partial class MonthlyAggregatedPrice
{
public int CommodityId { get; set; }
public int MarketId { get; set; }
public int PriceTypeId { get; set; }
public int CommodityUnitId { get; set; }
public int CurrencyId { get; set; }
public string CommodityName { get; set; }
public string MarketName { get; set; }
public string PriceTypeName { get; set; }
public string CommodityUnitName { get; set; }
public string CurrencyName { get; set; }
public int Adm0Code { get; set; }
public string CountryISO3 { get; set; }
public string CountryName { get; set; }
public decimal CommodityPrice { get; set; }
public string OriginalFrequency { get; set; }
public string CommodityPriceSourceName { get; set; }
public int? CommodityPriceObservations { get; set; }
public int CommodityDateMonth { get; set; }
public int CommodityDateYear { get; set; }
public DateTime CommodityPriceDate { get; set; }
public string CommodityPriceFlag { get; set; }
}

它必须是 IQueryable,因为稍后我应该对数据应用更多过滤器

*** 更新 ***即使我尝试在第一个查询中显式创建对象,我也会收到以下错误:

“当匹配的两边的列有不同的存储类型时,无法翻译集合操作。”

IQueryable < MonthlyAggregatedPrice > monthlyAggregatedPrices = 
(from map in db.MonthlyAggregatedPrices
where map.Adm0Code == adm0Code
orderby map.CommodityPriceDate descending
select new MonthlyAggregatedPrice
{
CommodityId = map.CommodityId,
MarketId = map.MarketId,
PriceTypeId = map.PriceTypeId,
CommodityUnitId = map.CommodityUnitId,
CurrencyId = map.CurrencyId,
CommodityName = map.CommodityName,
MarketName = map.MarketName,
PriceTypeName = map.PriceTypeName,
CommodityUnitName = map.CommodityUnitName,
CurrencyName = map.CurrencyName,
Adm0Code = adm0Code,
CountryISO3 = countryInfo.Iso3Alpha3,
CountryName = countryInfo.Name,
CommodityPrice = map.CommodityPrice,
OriginalFrequency = map.OriginalFrequency,
CommodityPriceSourceName = map.CommodityPriceSourceName,
CommodityPriceObservations = map.CommodityPriceObservations,
CommodityDateMonth = map.CommodityDateMonth,
CommodityDateYear = map.CommodityDateYear,
CommodityPriceDate = map.CommodityPriceDate,
CommodityPriceFlag = map.CommodityPriceFlag
}).Union(
from f in db.ST_PewiPriceForecasts
join cm in db.Commodities on f.CommodityID equals cm.CommodityID
join m in db.Markets on f.MarketID equals m.MarketId
join u in db.CommodityUnits on f.CommodityUnitID equals u.CommodityUnitID
join pt in db.PriceTypes on f.PriceTypeID equals pt.PriceTypeID
join cu in db.Currencies on f.CurrencyID equals cu.CurrencyID
where f.Adm0Code == adm0Code
select new MonthlyAggregatedPrice
{
CommodityId = f.CommodityID,
MarketId = f.MarketID,
PriceTypeId = f.PriceTypeID,
CommodityUnitId = f.CommodityUnitID,
CurrencyId = f.CurrencyID,
CommodityName = cm.CommodityName,
MarketName = m.MarketName,
PriceTypeName = pt.PriceTypeName,
CommodityUnitName = u.CommodityUnitName,
CurrencyName = cu.CurrencyName,
Adm0Code = adm0Code,
CountryISO3 = countryInfo.Iso3Alpha3,
CountryName = countryInfo.Name,
CommodityPrice = 0,
OriginalFrequency = "monthly",
CommodityPriceSourceName = "",
CommodityPriceObservations = null,
CommodityDateMonth = f.PriceForecastMonth,
CommodityDateYear = f.PriceForecastYear,
CommodityPriceDate=dt,
CommodityPriceFlag = "forecast"
});

最佳答案

当我使用 Entity Framework 和 Oracle 遇到同样的问题时,我找到了一个简单的解决方案。为方便起见,我复制了@Phil A. 的部分答案。

(
from item in _context.Table1
select new SomeDto
{
// Some other fields trimmed for readability
UserName = Convert.ToString(item.UserName)
}
)
.Union
(
from item in _context.Table2
select new SomeDto
{
// Some other fields trimmed for readability
UserName = Convert.ToString(item.UserName)
}
)

即使我的实体已经是一个字符串,我也必须在两边进行转换,以便它们查看相同的数据类型。

关于c# - 与 EF Core 联合返回无法转换集合操作,因为两个操作数具有不同的 'Include' 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66789248/

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