Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
将以下sql查询转换为lambda表达式_Sql_Linq - Fatal编程技术网

将以下sql查询转换为lambda表达式

将以下sql查询转换为lambda表达式,sql,linq,Sql,Linq,如何将以下sql查询转换为lambda表达式 select cg.Code, ci.ChangeType, SUM(oc.Value) from OtherCharges oc left join changeitems ci on oc.ChangeItemKey = ci.ChangeItemKey left join ChangeGroups cg on ci.ChangeGroupKey = cg.ChangeGroupKey where OtherKey = 'AB235A00-FE

如何将以下sql查询转换为lambda表达式

select cg.Code, ci.ChangeType, SUM(oc.Value) from OtherCharges oc 
left join changeitems ci on oc.ChangeItemKey = ci.ChangeItemKey
left join ChangeGroups cg on ci.ChangeGroupKey = cg.ChangeGroupKey
where OtherKey = 'AB235A00-FEB2-4C4F-B0F9-3239FD127A8F'
group by cg.Code, ci.ChangeType
order by cg.Code, ci.ChangeType

假设您的表已经有.NET域类型:

IQueryable<OtherCharges> otherCharges = ...
Guid otherKey = ...

var query = otherCharges.Where(oc => oc.OtherKey == otherKey)
    .Select(oc => new { oc.ChangeItem, oc.Value })
    .GroupBy(t => new { t.ChangeItem.ChangeGroup.Code, t.ChangeItem.ChangeType })
    .OrderBy(g => g.Key.Code)
    .ThenBy(g => g.Key.ChangeType)
    // note that if Code is a non-nullable type you'll want to cast it to int? at some
    // point so that when pulled into memory EF won't complain that you can't cast
    // null to a non-nullable type. I expect that Code could sometimes be null here
    // do to your use of LEFT OUTER JOIN in the T-SQL
    .Select(g => new { g.Key.Code, g.Key.ChangeType, Sum = g.Sum(t => t.Value) });

var inMemoryResult = query.ToList();
IQueryable其他费用=。。。
Guid otherKey=。。。
var query=otherCharges.Where(oc=>oc.OtherKey==OtherKey)
.Select(oc=>new{oc.ChangeItem,oc.Value})
.GroupBy(t=>new{t.ChangeItem.ChangeGroup.Code,t.ChangeItem.ChangeType})
.OrderBy(g=>g.Key.Code)
.ThenBy(g=>g.Key.ChangeType)
//请注意,如果代码是不可为null的类型,您是否希望将其强制转换为int?有时
//点,这样当被拉入内存时,EF不会抱怨你不能施放
//null为不可为null的类型。我希望代码在这里有时是空的
//在T-SQL中如何使用左外连接
.Select(g=>new{g.Key.Code,g.Key.ChangeType,Sum=g.Sum(t=>t.Value)});
var inMemoryResult=query.ToList();
注意,我在这里使用的是OtherCharge.ChangeItem和ChangeItem.ChangeGroup。这些是关联属性,需要作为模型的一部分进行设置(例如,首先对EF代码使用fluent配置)