C# 在C中将sql查询转换为linq

C# 在C中将sql查询转换为linq,c#,sql,linq-to-entities,C#,Sql,Linq To Entities,嗨,有人能帮我用Linq C写下面的查询吗 select YearValue from [Information].Year where YearId in (select max(YearId) from curreny where BudgetCodeId = 2) 以下是我尝试过的: var maxYear = from year in dbContext.Years join exchange in dbContext.CurrencyExchangeRates

嗨,有人能帮我用Linq C写下面的查询吗

select YearValue from [Information].Year 
where YearId in (select max(YearId) from curreny where BudgetCodeId = 2)
以下是我尝试过的:

var maxYear = 
    from year in dbContext.Years 
    join exchange in dbContext.CurrencyExchangeRates 
    on year.YearId equals exchange.YearId 
    where exchange.BudgetCodeId == budgetCodeId 
    orderby year.YearValue descending 
    select new { yearValue = year.YearValue };

这就是你要找的:

var maxYear = from year in dbContext.Years where (from exchange in dbContext.CurrencyExchangeRates where exchange.BudgetCodeId == 2 select exchange)
    .Max(m => m.YearId) == 
        year.YearId select year.YearValue;

int mYear = maxYear.First();

到目前为止你试过什么?这不是一个代码编写服务。我试过这样做,但没有得到想要的输出。var maxYear=dbContext中的from year.Years加入dbContext.currencyExchanges于year.YearId等于exchange.YearId,其中exchange.BudgetCodeId==BudgetCodeId orderby year.YearValue降序选择新建{yearValue=year.yearValue};这与您所追求的SQL完全不同。什么是货币兑换率?为什么您要按年份值订购?我想从年份表中获取年份值,其中yearId取自currencyexchangeRate表,并且该年份id应为最大值您听说过或尝试过查看LinqPad吗。我发现它对这样的事情很有用。