Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将linq模型转换为通用列表_C#_Linq_Linq To Sql_Generics - Fatal编程技术网

C# 将linq模型转换为通用列表

C# 将linq模型转换为通用列表,c#,linq,linq-to-sql,generics,C#,Linq,Linq To Sql,Generics,我有一个现有的类映像,它在我的应用程序中被广泛使用。 我需要将图像的一般列表(list)返回到前端,但由于我正在查询的3ed party DB中没有存储过程,我需要使用Linq to Sql 我已经为我在DAL中查询的数据库创建了一个dbtm文件,如下所示: ImageCat ImageId Name Width DateModified Height CatId 我的图像类如下 public class Image { public int

我有一个现有的类映像,它在我的应用程序中被广泛使用。 我需要将图像的一般列表(list)返回到前端,但由于我正在查询的3ed party DB中没有存储过程,我需要使用Linq to Sql

我已经为我在DAL中查询的数据库创建了一个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);   
}
var imageList=(来自db.ImageCats中的ic
其中ic.CatID.包含(类别)
选择ic.ToList();
var finalImageList=新列表();
foreach(imageList中的ImageCat ic)
{
图像=新图像();
image.Id=ic.ImageID;
image.Height=(int)ic.Height;
image.Name=ic.Name;
image.Width=(int)ic.Width;
添加(图像);
}

我不想循环使用linq到Sql结果来设置我的列表。有更简单的方法吗。什么是最佳实践?我不喜欢将我的dbml类公开到表示层的想法。

您可以在LINQ查询中直接选择
Image

var imageList = (
    from ic in db.ImageCats
    where ic.CatID.Contains(category)
    select new Image()
    {
         Id= ic.ImageID,
         Height = (int)ic.Height,
         Name = ic.Name,
         Width = (int)ic.Width,
    }
).ToList();
你可以这样做:

var imageList = db.ImageCats.Where(ic => ic.CatID.Contains(category))
.Select(ic => new Image{ Id = ic.ImageID, 
                         Height = (int)ic.Height, 
                         Name = ic.Name, 
                         Width = (int)ic.Width})
.ToList(); 
IEnumerable=来自ic,单位为db.ImageCats
其中ic.CatID.包含(类别)
选择new Image(){Id=ic.ImageID,Height=(int)ic.Height,Name=ic.Name,Width=(int)ic.Width}.ToList();

我想沿着这些思路,你会得到一个充满图像对象的IEnumerable。我在编辑窗口中写下了这句话,这样我就可以走远了。请尝试一下,并告诉我它是否适合您。

大约有三个错误阻止了此代码的编译。现在查看这些错误,以及选定的答案,我看到了它们。我想在修好这些之后,可能还会出现更多的问题。很抱歉代码很糟糕(正如在编辑器中编写的那样),但是想法很好:PWorks可以,但是您将在此过程中创建一组临时ImageCat对象。
IEnumerable<Image> = from ic in db.ImageCats
                     where ic.CatID.Contains(category)
                     select new Image() { Id= ic.ImageID, Height = (int)ic.Height, Name = ic.Name, Width = (int)ic.Width }.ToList();