简体   繁体   English

如何使用linq和C#中的实体框架从另一个表中获取对象中的项目列表?

[英]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(); 公共响应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;
    }

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. 我想做的是,我想从另一个表中获取详细信息,该表具有多行包含与wOrder相关的数据,并将其返回到列表类型对象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
                           };   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用LINQ或Entity Framework将数据分割为列出对象C# - Split datatable to list object C# using LINQ or Entity Framework 从数据库获取所有表,并使用C#,Linq查询和Entity Framework遍历每个表 - Get all tables from database and loop through each table using C#, Linq query and Entity Framework 如何使用linq C#使用实体框架获取两个表合并记录? - how to get two table combine record using entity framework with linq c#? C#实体框架,如何从通过Junction表创建的对象中获取主键 - C# Entity framework, how to get the primary key from an object created from a Junction table 使用实体框架linq从数据库结果中获取项目 - Get item from database result by using entity framework linq 如何在使用linq和C#的查询中创建一个包含另一个对象类型列表的列表对象 - How do I create a list object that contains a list of another object type using from within a query using linq and c# C#LINQ如何从另一个列表中的一个列表中找到匹配项,并在另一个列表中检查属性 - C# LINQ How to find matching item from one list in another list and check for the property in the other list 如何使用 linq 对 Entity Framework 中的列求和并将其与另一个表连接 - How sum a column in Entity Framework using linq and join it with another table c#linq如果项目在列表中并从列表中获取匹配的项目 - c# linq if item is in list and get matching items from list 如何使用Linq和C#在实体框架中获取多个结果集? - How to get Multiple Result Set in Entity Framework using Linq with C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM