linq如何从var中获取值

时间:2012-07-06 21:30:04

标签: linq c#-4.0

如何从以下LINQ查询中获取单个值? (我想得到DecalExpireDate,DecalExpireMonth和DecalExpireYear的值。)

var previousExpirationDate = (from d in db.CT_Decals
                              where d.TankID == decal.TankID
                              && d.DecalStatus == "Approved"
                              select new
                              {
                                  d.DecalExpireDate,
                                  d.DecalExpireMonth,
                                  d.DecalExpireYear
                              }).Max(d => d.DecalExpireDate);

4 个答案:

答案 0 :(得分:11)

我假设您希望从具有最高(最后)DecalExpireDate的元素中获取DecalExpireMonthDecalExpireYearDecalExpireDate。然后你可以按这个日期订购:

var latest = (from d in db.CT_Decals
             where d.TankID == decal.TankID && d.DecalStatus == "Approved"
             orderby d.DecalExpireDate descending
             select new
             {
                 d.DecalExpireDate,
                 d.DecalExpireMonth,
                 d.DecalExpireYear
              }).First();
var decalExpireDate  = latest.DecalExpireDate;
var decalExpireMonth = latest.DecalExpireMonth;
var decalExpireYear  = latest.DecalExpireYear;

答案 1 :(得分:0)

做出与蒂姆相同的假设,你也可以选择

var result = db.CT_Decals.OrderByDescending(d => d.DecalExpireDate).First(d => d.TankID == decal.TankID && d.DecalStatus == "Approved");

var expireDate = result.DecalExpireDate;
var expireMonth = result.DecalExpireMonth; 
var expireYear = result.DecalExpireYear;

您将在'result'变量中拥有所需的'CT_Decals'类型的对象,以便访问它的任何属性

答案 2 :(得分:0)

var q = from r in tab1.dictenaries
                    select new {
                        r.ENGLISH,
                        r.FRENCH
                    };

            List<SomeClass> sml = new List<SomeClass>();

            int starting = 10;
            int ending = 20;
            int cntr = 0;
           foreach( var verObj in q)
           {
               if (cntr >= starting && cntr <= ending)
               {
                   SomeClass sm1 = new SomeClass();
                   sm1.ENGLISH = verObj.ENGLISH.ToString();
                   sm1.FRENCH = verObj.FRENCH.ToString();
                   sml.Add(sm1);
               }
               else if(cntr >ending)
               {
                   break;
               }
               cntr = cntr + 1;
           }

答案 3 :(得分:0)

在Tim和Dipin上构建时,First()方法在查询没有产生任何结果时抛出InvalidOperationException。

要么用try / catch块包围它

try{
    var latest = (from d in db.CT_Decals
                 where d.TankID == decal.TankID && d.DecalStatus == "Approved"
                 orderby d.DecalExpireDate descending
                 select new
                 {
                     d.DecalExpireDate,
                     d.DecalExpireMonth,
                     d.DecalExpireYear
                  }).First();
    var decalExpireDate  = latest.DecalExpireDate;
    var decalExpireMonth = latest.DecalExpireMonth;
    var decalExpireYear  = latest.DecalExpireYear;
}
catch(InvalidOperationException ex){ // tell no Result!}

或者您可以使用FirstOrDefault(),当查询产生不匹配时,它将返回空引用。

//... Example from above

}).FirstOrDefault();

// check for null-reference
if(latest != null)
    var decalExpireDate  = latest.DecalExpireDate;
    var decalExpireMonth = latest.DecalExpireMonth;
    var decalExpireYear  = latest.DecalExpireYear;
}

第三种可能性是使用Take(1)

获取第一个元素
//... Example from above

}).Take(1).ToList();  // returns an empty list if no match

// check whether something was returned
if(latest.Any())
    var decalExpireDate  = latest[0].DecalExpireDate;
    var decalExpireMonth = latest[0].DecalExpireMonth;
    var decalExpireYear  = latest[0].DecalExpireYear;
}