我有一個現有的類圖像,在我的應用程序中廣泛使用。 我需要返回一個通用的圖像列表(列表)到前端,但因爲沒有存儲過程在3ed方數據庫我正在問我需要使用Linq到Sql。將LINQ模型轉換爲通用列表
我創造,我在我的DAL quering數據庫,它看起來像一個DBTM文件:
ImageCat
ImageId
Name
Width
DateModified
Height
CatId
我的圖像類是如下
public class Image
{
public int Id { get; set; }
public string Name { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
我的LINQ到SQL是如下:
var imageList = (from ic in db.ImageCats
where ic.CatID.Contains(category)
select ic).ToList();
var finalImageList = new List<Image>();
foreach (ImageCat ic in imageList)
{
Image image = new Image();
image.Id= ic.ImageID;
image.Height = (int)ic.Height;
image.Name = ic.Name;
image.Width = (int)ic.Width;
finalImageList.Add(image);
}
我不想通過linq sql循環結果來設置我的列表。有更容易的方法嗎。什麼是最佳做法?我不喜歡將我的dbml類暴露給表示層的想法。
非常感謝,效果很好 – skyfoot 2010-10-12 11:50:20