簡體   English   中英

如何使用linq和C#中的實體框架從另一個表中獲取對象中的項目列表?

[英]how to get a list of item in object from another table using linq and entity framework in C#?

在此處輸入圖片說明 以下是我正在使用的以下代碼。 如附件屏幕截圖所示,我遇到了錯誤。

      public class BioHazardAffectedPropertyDetails
{
    public int BiohazardID { get; set; }
    public string WONumber { get; set; }
    public string BiohazardName { get; set; }
    public string PropertyAddress { get; set; }
    public string Name { get; set; }
    public string Phone { get; set; }
    public string Address { get; set; }
    public string Signature { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
    public IEnumerable<FormItem> formItemList { get; set; } 
}

公共響應Get(字符串wOrder){響應res =新的Response();

        if (string.IsNullOrEmpty(wOrder))
            throw new ArgumentNullException(wOrder);

        try
        {
            using (HoarderDBEntities db = new HoarderDBEntities())
            {
                List<HoarderApi.Models.BiohazardForm> oBiohazardForm = new List<HoarderApi.Models.BiohazardForm>();
                List<HoarderApi.Models.FormItem> objForm = new List<HoarderApi.Models.FormItem>();
                var bioH = from ob in db.BiohazardForms
                           join fi in db.FormItems on ob.WONumber equals fi.WONumber
                           where ob.WONumber == fi.WONumber
                           select new BioHazardAffectedPropertyDetails
                           {
                               BiohazardName = ob.BiohazardName,
                               PropertyAddress = ob.PropertyAddress,
                               Date = ob.Date,
                               Phone = ob.Phone,
                               Address = ob.Address,
                               Name = ob.Name,
                               Signature = ob.Signature,
                               formItemList = db.FormItems.Where(x => x.WONumber == ob.WONumber).ToList<FormItem>()
                           };                  

                //foreach (FormItem fii in oFI)
                //{
                //    bioH = obioHForm.Zip(oFI, (obiohform, ofi) => new { obiohform.BiohazardName, obiohform.PropertyAddress, obiohform.Date, obiohform.Phone, obiohform.Name, obiohform.Signature, fii.ItemsID, fii.ItemDesc, fii.Quantity, fii.IfKept, fii.Initial, fii.IsBiohazard }).ToList(); 
                //}                    
                //res.status.success = true;                                
                //[![enter image description here][1]][1]res.data = bioH;
            }
        }
        catch (Exception ex)
        {
            res = new Response(ex);
        }
        return res;
    }

我想做的是,我想從另一個表中獲取詳細信息,該表具有多行包含與wOrder相關的數據,並將其返回到列表類型對象formItemList。 但是當它執行以下代碼時,它將在所附的圖像中返回以下錯誤。任何人都可以簡化此過程或為我提供幫助。

嘗試這個 :

var bioH = from ob in db.BiohazardForms
                           join fi in db.FormItems on ob.WONumber equals fi.WONumber into g
                           select new BioHazardAffectedPropertyDetails
                           {
                               BiohazardName = ob.BiohazardName,
                               PropertyAddress = ob.PropertyAddress,
                               Date = ob.Date,
                               Phone = ob.Phone,
                               Address = ob.Address,
                               Name = ob.Name,
                               Signature = ob.Signature,
                               formItemList = g
                           };   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM