2009-05-11 5 views
0

私は個人的なプロジェクトに取り組んでいましたが、Outlookからアイテムを抽出し、WCFで "RESTful"なデザインでホストする簡単なサービスが必要でした。その過程で、私はこのやや野生のクラスを考え出しました。あなたが見た最も恐ろしいLINQ機能は何ですか?

他の恐ろしいlinqコードは見ましたか?

public IQueryable<_AppointmentItem> GetAppointments(DateTime date) 
{ 
    var dayFlag = (OlDaysOfWeek)(int)Math.Pow(2, (int)date.DayOfWeek); 
    return 
     OlDefaultFolders.olFolderCalendar.GetItems<_AppointmentItem>() 
     .Select(a => new 
     { 
      Appointment = a, 
      RecurrencePattern = a.IsRecurring ? 
           a.GetRecurrencePattern() : null 
     }) 
     .Where(a => 
      a.Appointment.Start.Date <= date && 
      (
       (a.RecurrencePattern == null && 
       a.Appointment.End.Date >= date) || 
       (
        a.RecurrencePattern != null && 
        (
         (a.RecurrencePattern.DayOfMonth == 0 || 
         a.RecurrencePattern.DayOfMonth == date.Day) && 
         (a.RecurrencePattern.DayOfWeekMask == 0 || 
         ((a.RecurrencePattern.DayOfWeekMask & 
          dayFlag) != 0)) && 
         (a.RecurrencePattern.MonthOfYear == 0 || 
          a.RecurrencePattern.MonthOfYear == date.Month) 
        ) 
       ) 
      ) 
     ) 
     .Select(a => a.Appointment); 
} 

[OperationContract()] 
[WebGet(
    UriTemplate = "/appointments/{year}/{month}/{day}", 
    RequestFormat = WebMessageFormat.Xml, 
    ResponseFormat = WebMessageFormat.Xml, 
    BodyStyle = WebMessageBodyStyle.Bare 
    )] 
[ContentType("text/xml")] 
public XElement ListAppointments(string year, string month, string day) 
{ 
    try 
    { 
     int iYear, iMonth, iDay; 
     int.TryParse(year, out iYear); 
     int.TryParse(month, out iMonth); 
     int.TryParse(day, out iDay); 

     if (iYear == 0) iYear = DateTime.Now.Year; 
     if (iMonth == 0) iMonth = DateTime.Now.Month; 
     if (iDay == 0) iDay = DateTime.Now.Day; 

     var now = new DateTime(iYear, iMonth, iDay).Date; // DateTime.Now; 
     return GetAppointments(now).ToXml(); 
    } 
    catch (System.Exception ex) 
    { 
     return new XElement("exception", ex.ToString()); 
    } 
} 


using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Xml.Linq; 
using Microsoft.Office.Interop.Outlook; 

namespace WhitedUS.ServiceModel.Office.Linq 
{ 
    public static class OutlookUtilities 
    { 
     public static IQueryable<T> GetItems<T>(
      this OlDefaultFolders defaultFolderType) 
     { 
      return 
       new ApplicationClass() 
       .Session 
       .GetDefaultFolder(defaultFolderType) 
       .Items 
       .OfType<T>() 
       .AsQueryable(); 
     } 

     public static XElement ToXml<T>(this IEnumerable<T> input) 
     { 
      if (input == null) 
       return null; 

      Type typ = typeof(T); 
      var root = XName.Get(typ.Name.Trim('_')); 

      return new XElement(root, 
       input 
       .Select(x => x.ToXml<T>()) 
       .Where(x => x != null) 
       ); 
     } 

     public static XElement ToXml<T>(this object input) 
     { 
      if (input == null) 
       return null; 

      Type typ = typeof(T); 
      var root = XName.Get(typ.Name.Trim('_')); 

      return new XElement(root, 
       typ.GetProperties() 
       .Where(p => p.PropertyType.IsValueType || 
         p.PropertyType == typeof(string)) 
       .Select(p => new { Prop = p, Getter = p.GetGetMethod() }) 
       .Where(p => p.Getter != null) 
       .Select(p => new { Prop = p.Prop, Getter = p.Getter, 
            Params = p.Getter.GetParameters() }) 
       .Where(p => (p.Params == null || p.Params.Count() <= 0)) 
       .Select(p => new { Name = p.Prop.Name, 
            Value = p.Getter.Invoke(input, null) }) 
       .Where(p => p.Value != null) 
       .Select(p => new XAttribute(XName.Get(p.Name), p.Value)) 
       ); 
     } 
    } 
} 

も参照してください:What is the worst abuse you’ve seen of LINQ syntax?

答えて

0

実際、私が今までに見た中で最も恐ろしいLinqクエリは、私の最初のものでした。それは、私がそれを理解していると思ってもらえて、私が本当にやったことを疑うようにするのに十分なだけのものでした。

関連する問題