当前位置:  开发笔记 > Android > 正文

你见过的最可怕的LINQ功能是什么?

如何解决《你见过的最可怕的LINQ功能是什么?》经验,为你挑选了1个好方法。

在处理个人项目时,我想要一个简单的服务,从Outlook中提取项目,并在"RESTful"设计中在WCF中托管.在这个过程中,我提出了这个相当野兽的课程.

人们见过的其他可怕的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 GetItems(
            this OlDefaultFolders defaultFolderType)
        {
            return
                new ApplicationClass()
                .Session
                .GetDefaultFolder(defaultFolderType)
                .Items
                .OfType()
                .AsQueryable();
        }

        public static XElement ToXml(this IEnumerable 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())
                .Where(x => x != null)
                );
        }

        public static XElement ToXml(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))
                );
        }
    }
}

另请参阅:您在LINQ语法中看到的最糟糕的滥用是什么?



1> Igal Tabachn..:

完全LINQified RayTracer非常可怕.

推荐阅读
手机用户2502851955
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有