如何将两个实体框架调用合并为一个?

时间:2017-02-15 22:44:12

标签: c# entity-framework linq

所以我有以下代码:

    var MAX_VOLUME = (from b in context.TRANSPORT_PLANNER_TRACK_CAP.Where(b => b.WAREHOUSE.Equals(warehouse)) select b.MAX_VOLUME).SingleOrDefault();
    var MAX_WEIGHT = (from b in context.TRANSPORT_PLANNER_TRACK_CAP.Where(b => b.WAREHOUSE.Equals(warehouse)) select b.MAX_WEIGHT).SingleOrDefault();
string maxVolume = MAX_VOLUME.ToString();
string maxWeight = MAX_WEIGHT.ToString();

我想将这两个语句组合成一个EF调用,以减少冗余。什么是最好的方法?

1 个答案:

答案 0 :(得分:5)

var result = (from b in context.TRANSPORT_PLANNER_TRACK_CAP
              where b.WAREHOUSE.Equals(warehouse)
              select new { b.MAX_VOLUME, b.MAX_WEIGHT}).SingleOrDefault();

然后将其引用为:

result.MAX_WEIGHT or result.MAX_VOLUME