返回选择指定的列 [英] Return selected specified columns
问题描述
我想从某个(BLOB)的表只选择几个列。我有这样的字段:ID,RowVersion,大小,签名,斑点,我想只选择第4位。我这样做,是这样的:(--->是一个错误的地方)
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)
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; }
}
当我试图返回BlobDetails错误occures - 作为VS.08没有按知道如何从匿名类型(allBlobs)转换到列表。
Error occures when I am trying to return BlobDetails - as VS.08 doesn't know how to convert from Anonymous Type (allBlobs) to List.
我不想选择所有值,因为Blob字段可以是相当沉重的,我不希望所有的时间发送。
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.
你有什么想法如何正确地做呢?
Do you have any idea how to do it properly?
推荐答案
如果 BlobDetails
不是的LINQ实体,那么你可以直接做到这一点:
If BlobDetails
isn't the LINQ entity, then you can do it directly:
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();
不过,如果 BlobDetails
是 LINQ的实体,你需要使用subtrefuge:
However; if BlobDetails
is a LINQ entity, you need to use 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();
的
AsEnumerable
打破了LINQ构图,使得它的工作。
The AsEnumerable
breaks the LINQ composition, making it work.
这篇关于返回选择指定的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!