1.当需要对返回的结果进行操作时(比如foreach()操作),可以将这个结果先ToList(),这样可以提高程序运行速度。
2.当不需要对结果进行操作时,直接返回IEnumerable对象即可。
decimal? result = a==0?null:b 这里b 必须是decimal?类型,否则在?null处会报错。
linq中可以直接使用Count()等函数,不用考虑前面的对象是否为null
public static double
NumberOfWeeks(DateTime dateStart, DateTime
dateEnd)
{
double
WeekCount =
0;
TimeSpan Span =
dateEnd.Subtract(dateStart);
if (Span.Days <=
7)
return 1;
else
WeekCount = (double)Span.Days /
7;
return WeekCount;
}
public static int
WeekOfYear(DateTime date)
{
GregorianCalendar cal = new
GregorianCalendar(GregorianCalendarTypes.Localized);//using
System.Globalization
return cal.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
}
public static DateTime
BeginningOfWeek(DateTime date)
{
DayOfWeek day = date.DayOfWeek;//using
System
int
days = day -
DayOfWeek.Monday;
if (days <
0)
days =
6;
return date.AddDays(-days);
}
原文地址:http://www.cnblogs.com/zjf1987/p/linq.html