4

如何从以下 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

4 回答 4

11

我假设您想从具有最高 (last) 的元素中获取DecalExpireDate, DecalExpireMonth, and 。然后您可以在此日期之前订购:DecalExpireYearDecalExpireDate

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;
于 2012-07-06T21:37:24.087 回答
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;

您将在“结果”变量中拥有所需的“CT_Decals”类型对象,因此可以访问它的任何属性

于 2012-07-06T21:57:07.900 回答
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;
           }
于 2013-08-12T12:02:47.287 回答
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()which 在查询不匹配时返回空引用。

//... 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;
}
于 2016-02-18T07:54:06.533 回答