通过循环匿名类型删除图片文件

时间:2015-05-09 12:43:15

标签: c# asp.net anonymous-types

我正在尝试通过使用LINQ to SQL匿名类型检索虚拟路径从服务器删除物理照片文件,但是无法正常工作。我使用的是ASP.NET 4.5和Entity-framework 5.下面是我的代码:

        //deleting all image gallery file collection
        using (ProductModelEntities DB = new ProductModelEntities())
        {

            var picsDB = from pk in DB.Pictures
                            where pk.MainId == _id
                            select new
                            {
                                pk.ImageUrl
                            };

            foreach (var picUrl in picsDB)
            {
                string fileNme = Server.MapPath(picUrl.ToString());
                System.IO.File.Delete(fileNme);
            }
        }

1 个答案:

答案 0 :(得分:2)

对象不仅仅是URL,它还是一个具有名为ImageUrl的属性的对象,其中包含URL。您需要从属性中获取URL:

string fileNme = Server.MapPath(picUrl.ImageUrl);