简体   繁体   English

如何合并两个不同对象的列表?

[英]How to merge two lists of different objects?

I have two Data Entity Classes of different objects like- 我有两个不同对象的数据实体类,例如-

class UserDetails
{
   int Id,
   DateTime DoB,
   string Name,
   .....
   .....
   .....
}

class Documents 
{
   int Id,
   int UserIdFK,
   string ImagePath,
   .....
   .....
}

I am fetching data from below LINQ query- 我正在从下面的LINQ查询中获取数据-

var UserInfo = (from UserDetail in _userDBContext.UserDetails
                                             join doc in _userDBContext.Documents on UserDetail.Id equals doc.UserIdFK
                                             select new { UserDetail, doc }).ToList();

Now I get data in UserInfo List but data is in 2 rows, 1st row for UserDetails & 2nd one for Documents, how can I merge them (userInfo & Document data) into one single list, having only one row. 现在,我在UserInfo列表中获取数据,但数据分为2行,UserDetails的第一行和Documents的第二行,如何将它们(userInfo和Document数据)合并到一个只有一个行的单个列表中。 So that I could use that list in Razor Pages(Front end). 这样我就可以在Razor Pages(前端)中使用该列表。

I even created another object & then tried to Convert UserInfo List items into new list but unable to do so, what are the ways to get selected objects of both tables into one single list. 我什至创建了另一个对象,然后尝试将UserInfo List项目转换为新列表,但无法这样做,将两个表的选定对象合并到一个列表中的方法是什么。 Please help me to find an elegant solution. 请帮助我找到一个优雅的解决方案。

    public class UserKycCompositeModel
    {
       int Id,
       DateTime DoB,
       string Name,
       .......
       .......
       .......
       int Id,
       int UserIdFK,
       string ImagePath,
       .........
       .........
       .........
  }

If there is a many-to-one relationship between the Documents class and the UserDetails class (meaning that a UserDetails class can have multiple Documents ), then it would be better to give the UserDetails class a list of Documents 如果Documents类和UserDetails类之间存在多对一的关系(这意味着UserDetails类可以具有多个Documents ),那么最好给UserDetails类提供一个Documents列表。

Add a List<Documents> to the UserDetails class: List<Documents>添加到UserDetails类:

public class UserDetails
{
    public List<Documents> Documents { get; set; }
}

Select the unique UserDetails first: 首先选择唯一的UserDetails

var userDetails = from ud in _userDBCOntext.UserDetails
                  select ud;

This will result in an IEnumerable<UserDetails> . 这将导致IEnumerable<UserDetails> Loop through that IEnumerable and get all the Documents for each UserDetails instance: 遍历该IEnumerable并获取每个UserDetails实例的所有Documents

foreach (var userDetail in userDetails)
{
    userDetail.Documets = from doc in _userDBContext.Documents
                          where userDetail.Id equals doc.UserIdFK
                          select doc;
}

Now you have an IEnumerable<UserDetails> (named userDetails ) that has all of the Documents that correspond to it. 现在,您将拥有一个IEnumerable<UserDetails> (名为userDetails ),其中包含userDetails对应的所有Documents

It is always better to have a specific ViewModel created to bind it to the View. 最好创建一个特定的ViewModel将其绑定到View。 Window's answer from above should work for you. Window的回答应该对您有用。 If you still want to have a generic list instead, a slight tweak to the query should help you out. 如果您仍然希望拥有通用列表,则对该查询稍作调整应该可以为您提供帮助。

    var UserInfo = (from UserDetail in _userDBContext.UserDetails
                    join doc in _userDBContext.Documents 
                    on UserDetail.Id equals doc.UserIdFK
                    select new
                    {
                        UserId = UserDetail.Id,
                        DoB = UserDetail.DoB,
                        Name = UserDetail.Name,
                        DocId = doc.Id,
                        doc.UserIdFK,
                        doc.ImagePath
                    }).ToList();

Select the specific object properties into your composite object: 选择特定的对象属性到您的复合对象中:

List<UserKycCompositeModel> userKycCompositeModels = (from UserDetail in _userDBContext.UserDetails
    join doc in _userDBContext.Documents on UserDetail.Id equals doc.UserIdFK
    select new UserKycCompositeModel { UserDetail.Id, UserDetail.Dob, **...etc.** }).ToList();

Because var UserInfo is a List. 因为var UserInfo是一个List。 You cant/should create an object with properties that are not defined. 您不能/应该使用未定义的属性创建对象。

If you persist with this approach you can create the anonymus object with the way that arun Mohan say. 如果您坚持使用这种方法,则可以使用arun Mohan所说的方式创建匿名对象。

new {
   UserId = UserDetail.Id,
   DoB = UserDetail.DoB,
   Name = UserDetail.Name,
   DocId = doc.Id,
   doc.UserIdFK,
   doc.ImagePath
}

I don't recomend to use a new class UserKycCompositeModel as that way, but is better (as performance and controling) that having dynamic objects. 我不建议那样使用新的类UserKycCompositeModel,但是(在性能和控制方面)比具有动态对象更好。

Another thing if you want to avoid calling each property (like UserID = UserDetail.ID) is using reflection something like this: 如果您想避免调用每个属性(例如UserID = UserDetail.ID),则另一件事是使用类似以下的反射:

UserKycCompositeModel newObj = new UserKycCompositeModel();

foreach (var item in yourObject.GetType().GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly))
{
    propertyValue = item.GetValue(yourObject, null);
    Type myType = typeof(newObj);                   
    PropertyInfo myPropInfo = myType.GetProperty(item.Name);
    myPropInfo.SetValue(this, value, null);
}

There are a lot of ways, I believe, not to do exactly what you want, but depending on how you will use it in your logical business you can do it in a different way. 我相信,有很多方法不能完全按照自己的意愿去做,但是根据您在逻辑业务中如何使用它,您可以以其他方式来做。

I prefer to do: 我喜欢这样做:

public class UserKycCompositeModel
{
    public UserKycCompositeModel(UserDetails ud,Documents  doc)
    {
        this.UserDetails = ud;
        this.Document = doc;
    }
    public UserDetails UserDetails {get; private set;}
    public Documents Document {get; private set;}

}

And after that call in this way: 然后以这种方式调用:

var UserInfo = (from UserDetail in _userDBContext.UserDetails
                     join doc in _userDBContext.Documents on UserDetail.Id equals doc.UserIdFK
                     select new UserKycCompositeModel(UserDetail, doc)).ToList();

And use user info like UserInfo.UserDetails or UserInfo.Documents. 并使用用户信息,例如UserInfo.UserDetails或UserInfo.Documents。

There are more ways but maybe you have to explain why you want it toghter, or looks if you want to follow the normal way to use entities. 还有更多的方法,但也许您必须解释为什么要它摇摆,或者看看您是否要遵循使用实体的常规方法。

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

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