开发者

c# Turning a linq database record into a class instance

I'm not sure if this is the correct way to do things, it seems quite long winded and I want to check that this is a good way of turning a linq result into a class:

 /// <summary>
/// A job header
/// </summary>
public class JobHeader
{
    public int ID { get; set; }
    public int? UserID { get; set; }
    public int? ClientID { get; set; }
    public string JobName { get; set; }
    public string Code { get; set; }
    public DateTime? DateAdded { get; set; }
    public int? StatusID { get; set; }
    public bool UniversalQuantity { get; set; }
    public bool UniversalDelivery { get; set; }
    public bool UniversalDeliveryDates { get; set; }
    public bool BuyerHidden { get; set; }
    public bool FullfillmentDone { get; set; }
    public string TNTCode { get; set; }
    public int? ContactUserID { get; set; }
    public string ContactDepartment { get; set; }
    public string ConactName { get; set; }
    public DateTime? DateSubmitted { get; set; }
    public bool CampaignJob { get; set; }
    public bool UniversalBusinessUnits { get; set; }
    public bool TenderJob { get; set; }
    public Jobs.JobLine[] JobLines { get; set; }

    /// <summary>
    /// Create instance on db record ID
    /// </summary>
    public JobHeader(int? RecordID)
    {
        if (RecordID != null)
        {
            using (MainContext db = new MainContext())
            {
                var q = (from h in db.tblJobHeaders where h.id == Rec开发者_开发技巧ordID select h).SingleOrDefault();
                if (q != null)
                    LoadByRec(q);
            }
        }
    }
    public JobHeader(tblJobHeader Rec)
    {
        LoadByRec(Rec);
    }

    /// <summary>
    /// Sets properties to match database record
    /// </summary>
    private void LoadByRec(tblJobHeader Rec)
    {
        this.ID = Rec.id;
        this.UserID = Rec.userID;
        this.ClientID = Rec.ClientID;
        this.JobName = Rec.JobName;
        this.Code = Rec.JobCode;
        this.DateAdded = Rec.dateAdded;
        this.StatusID = Rec.statusID;
        this.UniversalQuantity = Rec.uniQty == 1;
        this.UniversalDelivery = Rec.uniDelivery == 1;
        this.UniversalDeliveryDates = Rec.uniDeliveryDates == 1;
        this.BuyerHidden = Rec.buyerHidden == 1;
        this.FullfillmentDone = Rec.fulfilmentDone == 1;
        this.TNTCode = Rec.jobTntCode;
        this.ContactUserID = Rec.JobClientContactID;
        this.ContactDepartment = Rec.JobClient;
        this.ConactName = Rec.JobAccountHandler;
        this.DateSubmitted = Rec.dateSubmitted;
        this.CampaignJob = Rec.isCampaignJob == 1;
        this.UniversalBusinessUnits = Rec.uniBusUnits == 1;
        this.TenderJob = Rec.isTenderJob == 1;
    }
}

Firstly I'm aware that the data source is not well formed (please ignore incorrect types etc etc), there's not much I can do about that at the moment. My query in specifically in regards to is this the best way to create an instance of a class from a Linq result? It becomes very long winded with a lot of properties.


Your approach is totally valid, if a little manual. I have worked on numerous projects that use this approach for creating C# objects from database queries. There are however a few technologies that can be used to generate types from queries:

  • Typed DataSets - a slightly dated approach
  • Linq to SQL
  • Entity Framework

All of the above can be used to generate types based on database schema.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜