【问题标题】:C# Linq create a list of derived class inside Group ByC# Linq 在 Group By 中创建派生类列表
【发布时间】:2020-02-14 15:28:50
【问题描述】:

我有一个方法,在该方法中我调用一个方法,该方法执行返回对象的 IEnumerable 的查询,然后我通过 Linq 对此 IEnumerable 执行分组,并详细说明分组的结果以将其插入到 JQGrid 中。在分组中,我创建了一个包含其他对象列表的对象。我希望这个列表不是基本类型,但我希望它是派生类型。 这是对执行查询的方法的调用,返回 IEnumerable 对象:

IEnumerable documentList = ObjBase.getAll(string.IsNullOrEmpty(columnList) ? null : columnList.Split(';'), sidx, sord, sqlWhere, out totalRows, userid: userId, siteId: siteId);

这是我在 Linq 分组后拥有的对象类别:

public class DocumentContact : ObjBase
{
    public long? CODE_CUSTOMER_ID { get; set; }
    public long? CODE_CUSTOMERTYPEID { get; set; }
    public long? CODE_CONTRACT_ID { get; set; }
    public long CODE_SITEID { get; set; }
    public string DESC_CONTACT_NUMBER { get; set; }
    public string DESC_CONTRACT_NUMBER { get; set; }
    public string DESC_CONTRACT_STATES { get; set; }
    public List<DocumentModelDates> Documents { get; set; }
}

这是 DocumentModel 类:

public class DocumentModel : ObjBase
{
    private long _id;
    private string _documentModel;
    private long? _documentModelTypeId;
    private long? _documentModelIssuingEntityId;
    private string _documentModelFilename;
    private string _documentModelPath;
    private DateTime? _documentModelStart;
    private DateTime? _documentModelEnd;
    private string _documentModelVersion;
    private long _contractId;
    private string _contractNumber;
    private long _contractTypeId;
    private long _contractStateId;
    private string _contractState;
    private long _documentModelStateId;
    private string _documentModelValoId;
    private DateTime? _documentModelUploadDate;
    private byte[] _documentModelContent;
    private long _documentModelFilter;
    private bool _flagDeleted;
    private long _userId;
    private int _orderDocumentModel = 0;
    private string _placeOfIssue;
    private string _issuingEntity;
    private DateTime? _releaseDate;
    private int? _idOpInsert;
    private int? _idOpId;
    private long? _customerId;
    private long? _customerTypeId;
    private string _customerNumber;
    private DateTime? _documentModelInvalidated;
    private bool _flagValidating;
    private string _fileRemotePath;
    private long? _folderId;
    private string _folderName;
    protected new HELPSI_Database HELPSI_Database = Common.HELPSI_Database.HELPSI_Anag;
}

这是 DocumentModelDates,派生自 DocumentModel:

    public class DocumentModelDates : DocumentModel
{
    public long? DateStart { get; set; }
    public long? DateEnd { get; set; }
    public long? DateUpload { get; set; }

    public DocumentModelDates(DocumentModel document)
    {
        this.CODE_DOCUMENT_MODEL_ID = document.CODE_DOCUMENT_MODEL_ID;
        this.DateStart = document.DATE_DOCUMENT_MODEL_START.Value.Ticks;
        this.DateEnd = document.DATE_DOCUMENT_MODEL_END.Value.Ticks;
        this.DateUpload = document.DATE_DOCUMENT_MODEL_UPLOAD.Value.Ticks;
    }
}

这是对这些 IEnumerable 的 Group By Linq 操作:

    var documentContact = documentList.GroupBy(c => new
    {
        c.CODE_CUSTOMERID,
        c.DESC_CONTACT_NUMBER,
        c.CODE_CUSTOMERTYPEID,
        c.CODE_CONTRACT_ID, 
        c.DESC_CONTRACT_NUMBER,
        c.DESC_CONTRACT_STATES,
    })
    .Select(gcs => new DocumentContact()
    {
        CODE_CUSTOMER_ID = gcs.Key.CODE_CUSTOMERID,
        DESC_CONTACT_NUMBER = gcs.Key.DESC_CONTACT_NUMBER,
        CODE_CUSTOMERTYPEID = gcs.Key.CODE_CUSTOMERTYPEID,
        CODE_CONTRACT_ID = gcs.Key.CODE_CONTRACT_ID,
        DESC_CONTRACT_NUMBER = gcs.Key.DESC_CONTRACT_NUMBER,
        DESC_CONTRACT_STATES = gcs.Key.DESC_CONTRACT_STATES,
        CODE_SITEID = siteId,
        Documents = gcs.ToList(),
    });

如果这样做,我会遇到转换错误:有没有办法在此 Select 中创建 DocumentModelDates 列表?谢谢。

【问题讨论】:

  • 试试Documents = gcs.Select(i =&gt; new DocumentModelDates(i)).ToList() ?

标签: c# linq


【解决方案1】:

你只需要:

.Select(gcs => new DocumentContact()
{
    //...
    Documents = gcs.Select(i => new DocumentModelDates(i)).ToList()
});

【讨论】:

    【解决方案2】:

    试试这样的

       public class DocumentModelDates
        {
            private DateTime _DateStart { get; set; }
            public long DateStart
            {
                get { return _DateStart.ToBinary(); }
                set { _DateStart = DateTime.FromBinary(value);}
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      相关资源
      最近更新 更多