LINQ重写博客垃圾图片回收算法_实用技巧

思路很简单,从所有Blog Model中解析出所有文章使用的图片文件名,排除站外引用,放入一个List<string> usedPicList。再遍历图片上传文件夹,把所有图片文件的结果加入FileInfo[] fiAllPicList。然后比较usedPicList和fiAllPicList,找出所有fiAllPicList中有,而usedPicList中木有的图片,就是未被任何文章引用的垃圾图片了。
原先这个比较算法是用传统方法写的,很蛋疼,用了两重循环,一个标志位才解决问题:

复制代码 代码如下:

List<FileInfo> garbagePicList = new List<FileInfo>();
for (int k = 0; k < fiAllPicList.Length; k++)
{
bool found = false;
for (int l = 0; l < usedPicList.Count; l++)
{
if (fiAllPicList[k].Name == usedPicList[l].ToString())
{
found = true;
}
}
if (!found)
{
garbagePicList.Add(fiAllPicList[k]);
}
}

今天用LINQ重写了一下:

复制代码 代码如下:

List<FileInfo> garbagePicList = new List<FileInfo>();
var query = from pic in fiAllPicList
where !usedPicList.Contains(pic.Name)
select pic;
garbagePicList = query.ToList();

清晰明了

时间: 2024-08-29 03:41:30

LINQ重写博客垃圾图片回收算法_实用技巧的相关文章

ASP.NET显示渐变图片实现方法_实用技巧

先给大家来个最终效果: 实现效果,首先准备一张图片,高度为25pixel,宽度为1至3pixel渐变的图片.可以这里下载. 还要准备数据: Dictionary<int, int> Datas { get { Dictionary<int, int> d = new Dictionary<int, int>(); d.Add(1, 35); d.Add(2, 45); d.Add(3, 20); return d; } } ok,数据准备完了,在aspx里放三个Labe

解析linq to xml操作XML的示例分析_实用技巧

.Net中的System.Xml.Linq命名空间提供了linq to xml的支持.这个命名空间中的XDocument,XElement以及XText,XAttribute提供了读写xml文档的关键方法.1. 使用linq to xml写xml:使用XDocument的构造函数可以构造一个Xml文档对象:使用XElement对象可以构造一个xml节点元素,使用XAttribute构造函数可以构造元素的属性:使用XText构造函数可以构造节点内的文本.如下实例代码: 复制代码 代码如下: cla

asp.net TripleDES加密、解密算法_实用技巧

using System;    using System.Collections.Generic;    using System.Linq;    using System.Text;    using System.Security.Cryptography;    using System.IO;    namespace WindowsFormsApplication1    {       #region TripleDES算法           public class Clas

asp.net用url重写URLReWriter实现任意二级域名 新_实用技巧

一般用百度搜的朋友都是对这个不了解但又急需要用的,我想,再多的语言也比不过一句代码.于是我把今天帮朋友时候写的一个小例子传了上来.这个小例子的目的是实现对任意url的重写(但不包括二级域名的,有需要二级域名的也可以先了解下url重写的概念). 这个小项目的制作过程如下 1.需要对诸如http://jb51.net/viewnews/2009/3/2.html的url进行重写.将其重写为http://jb51.net/viewnews.aspx?id=2&year=2009&month=3

asp.NET 脏字过滤算法_实用技巧

原文见http://www.jb51.net/article/20575.htm但在我这里测试的时候,RegEx要快一倍左右.但是还是不太满意,因为我们网站上脏字过滤用的相当多,对效率已经有了一些影响,经过一番思考后,自己做了一个算法.在自己的机器上测试了一下,使用原文中的脏字库,0x19c的字符串长度,1000次循环,文本查找耗时1933.47ms,RegEx用了1216.719ms,而我的算法只用了244.125ms. 更新:新增一个BitArray,用于判断某char是否在所有脏字中出现过

asp.net 获取文件夹中的图片的代码_实用技巧

前台: 复制代码 代码如下: <asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" CellSpacing="25"> <ItemTemplate> <img src="<%# Eval("FullName") %&

asp.net保存远程图片的代码_实用技巧

注意:并没有实现CSS中的图片采集,且图片的正则还有待完善. 复制代码 代码如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using

asp.net(c#)实现从sqlserver存取二进制图片的代码_实用技巧

下面说说主要实现思路: 1.存取图片 (1).将图片文件转换为二进制并直接存进sql server 复制代码 代码如下: //UploadHelper.cs /// <summary> /// 将图片转化为长二进制 /// </summary> /// <param name="photopath"></param> /// <returns></returns> public static Byte[] SetI

asp.net 图片超过指定大小后等比例压缩图片的方法_实用技巧

复制代码 代码如下: /// <summary>        /// 压缩图片        /// </summary>        /// <returns></returns>        public string ResizePic()        {            #region 压缩图片开始            bool IsImgFile = true;  //判断是否为图片文件            string file