如何将列表中的数据从数据库中选择到实体框架类?

时间:2017-06-05 20:44:36

标签: c# entity-framework

我想知道是否有人可以帮助我解决这个问题,我有Factorform这是一个主人和细节,我正在尝试从我的构造函数中的参数获取数据,我知道一个回合主人因为它是一个单一的选择但是关于细节,我得到了一个数据列表,以便我如何在我的类实体中获取它?

这是我的代码:

public partial class FactorForm : MetroFramework.Forms.MetroForm
{
        public FactorForm()
        {
            InitializeComponent();
        }

        public FactorForm(Guid _FactorID)
        {
            _FactorMode = (int)Factormode.update;
            FactorID = _FactorID;
            InitializeComponent();
        }
        //==============================================================================
        Factor _Factor;
        int _FactorMode = (int)Factormode.insert;
        Guid _WarhouseID;
        Guid _CustomerID;
        Guid _partref;
        public Guid FactorID { get; set; }

        enum Factormode
        {
            insert=1,
            update
        }
        //==============================================================================
        private void FactorForm_Load(object sender, EventArgs e)
        {
            CustomerGrid.Visible = false;
            WarehouseGrid.Visible = false;
            PartGrid.Visible = false;

            if (_FactorMode != (int)Factormode.insert && FactorID != null)
            {
                using (var context=new FactorEntities())
                {
                    var Factor = context.Factors.FirstOrDefault(f => f.FactorID==FactorID);
                    if (Factor != null)
                    {
                         _Factor.FactorID = Factor.FactorID;
                         _Factor.FactorCode = Factor.FactorCode;
                         _Factor.FactorDate = Factor.FactorDate;
                         _Factor.discount = Factor.discount;
                         _Factor.VAT = Factor.VAT;
                         _Factor.TotalPrice = Factor.TotalPrice;
                         _Factor.WarehouseRef = Factor.WarehouseRef;
                         _Factor.CustomerRef = Factor.CustomerRef;
                    }

                    var FactorItem = context.FactorItems.Where(fi => fi.FactorRef == FactorID);

                    if (FactorItem != null)
                    {
                        _Factor.
                    }
            }
        }
    }
}

那么这段代码应该是什么?

   if (FactorItem != null)
   {
       _Factor.
   }

提前致谢。

1 个答案:

答案 0 :(得分:0)

好吧,我明白了。我必须为每个人使用:

  var FactorItem = context.FactorItems.Where(fi => fi.FactorRef == FactorID);
                if (FactorItem!=null)
                {
                    foreach (var item in FactorItem)
                    {
                        _FactorItems.FactorItemID = item.FactorItemID;
                        _FactorItems.FactorRef = item.FactorRef;
                        _FactorItems.PartCount = item.PartCount;
                        _FactorItems.PartPrice = item.PartPrice;
                        _FactorItems.PartRef = item.PartRef;
                        _FactorItems.Rowno = item.Rowno;

                    }
                }