C# 返回选定的指定列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/819558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:26:50  来源:igfitidea点击:

Return selected specified columns

c#linqlinq-to-sql

提问by Michal Krawiec

I would like to select only few columns from a certain (Blobs) table. I have fields like: Id, RowVersion, Size, Signature, Blob, and I want to select only first four. I do it like this: (---> is an error place)

我只想从某个(Blob)表中选择几列。我有这样的字段:Id、RowVersion、Size、Signature、Blob,我只想选择前四个。我是这样做的:(---> 是一个错误的地方)

public List<BlobDetails> GetAllBlobsNames()  
{  
    RichTekstModelDataContext dc = new RichTekstModelDataContext();

    var allBlobs = from b in dc.Blobs
               orderby b.RowVersion descending
               select new {b.Id, b.Size, b.Signature, b.RowVersion};

---> allBlobs.ToList<BlobDetails>();
}

public class BlobDetails   
{  
    public int Id { get; set; }  
    public string Signature { get; set; }  
    public int Size { get; set; }  
    public System.Data.Linq.Binary RowVersion { get; set; }     
}

Error occures when I am trying to return BlobDetails - as VS.08 doesn't know how to convert from Anonymous Type (allBlobs) to List.

当我尝试返回 BlobDetails 时发生错误 - 因为 VS.08 不知道如何从匿名类型 (allBlobs) 转换为列表。

I do not want to select all values, because Blob field can be quite heavy and I don't want to send it all the time.

我不想选择所有值,因为 Blob 字段可能很重,我不想一直发送它。

Do you have any idea how to do it properly?

你知道如何正确地做到这一点吗?

采纳答案by Marc Gravell

If BlobDetailsisn'tthe LINQ entity, then you can do it directly:

如果BlobDetails不是LINQ 实体,则可以直接执行:

var qry = from b in dc.Blobs
          orderby b.RowVersion descending
          select new BlobDetails {
              Id = b.Id, Size = b.Size,
              Signature = b.Signature, RowVersion = b.RowVersion};

return qry.ToList();

However; if BlobDetailsisa LINQ entity, you need to use subtrefuge:

然而; 如果BlobDetailsLINQ 实体,则需要使用 subtrefuge:

var qry = from b in dc.Blobs
          orderby b.RowVersion descending
          select new {b.Id, b.Size, b.Signature, b.RowVersion};

var typedQry = from b in qry.AsEnumerable()
               select new BlobDetails {
                  Id = b.Id, Size = b.Size,
                  Signature = b.Signature, RowVersion = b.RowVersion};
return typedQry.ToList();

The AsEnumerablebreaks the LINQ composition, making it work.

AsEnumerable断LINQ组成,使其工作。

回答by Peter Lillevold

With "select new {" you are creating an anonymous type which cannot be implicitly cast to BlobDetails. Instead, explicitly declare the type you are newing within the select:

使用“select new {”,您将创建一个无法隐式转换为 BlobDetails 的匿名类型。相反,在选择中显式声明您要新建的类型:

var allBlobs = from b in dc.Blobs
           orderby b.RowVersion descending
           select new BlobDetails {Id = b.Id, .... };
allBlobs.ToList();