简体   繁体   中英

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

在此处输入图片说明 Below is the following code that i am using. i am getting error as shown in attached screenshot.

      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; } 
}

public Response Get(string wOrder) { Response res = new 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;
    }

what i am trying to do is that i want to fetch detail from another table which has multiple rows having data related to wOrder and return it to the list type object formItemList. but when it executes the following code then it is return the following error in the image attached.can anyone simplify this or help me.

Try this :

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
                           };   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM