Return selected specified columns

后端 未结 2 1352
半阙折子戏
半阙折子戏 2021-02-20 11:16

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 li

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-20 11:58

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

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

    The AsEnumerable breaks the LINQ composition, making it work.

提交回复
热议问题