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

Deleting picture files by looping through anonymous type

我正在尝试通过使用 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);
            }
        }

该对象不仅仅是 URL,它是一个包含 URL 且名为 ImageUrl 的 属性 的对象。您需要从 属性:

中获取 URL
string fileNme = Server.MapPath(picUrl.ImageUrl);