gpt4 book ai didi

c# - 返回选定的指定列

转载 作者:可可西里 更新时间:2023-11-01 09:02:37 25 4
gpt4 key购买 nike

我只想从某个 (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; }
}

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

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

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

最佳答案

如果 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();

但是;如果 BlobDetails LINQ 实体,则需要使用 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 组合,使其工作。

关于c# - 返回选定的指定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/819558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com