【问题标题】:How to bind linq data into Model class如何将 linq 数据绑定到模型类
【发布时间】:2021-07-29 16:36:40
【问题描述】:

我有以下查询,

var userTree = (from user in users
join location in locations on user.FkHomeLocationId equals location.LocationId
join region in regions on location.RegionId equals region.LocationId
group region by new {
    Regionid = region.LocationId,
    location.LocationId
}
into grp
select new {
    RegionName = regions.FirstOrDefault(s = >s.LocationId == grp.Key.Regionid).LocationName,
    Branches = new {
        BranchName = locations.FirstOrDefault(b = >b.RegionId == grp.Key.Regionid).LocationName,
        Users = users.Where(u = >u.FkHomeLocationId == grp.Key.LocationId).Select(s = >new {
            FullName = s.FirstName + " " + s.LastName
        }).ToList()
    }
}).ToList();

我想将查询结果绑定到模型中。我怎样才能做到这一点。我创建了以下类模型来实现这一点,

public class TreeModel
    {
        public string RegionName { get; set; }
        public Branch Branchs { get; set; }
    }

    public class Branch
    {
        public string BranchName { get; set; }
        public List<User> Users { get; set; }
    }

    public class User
    {
        public string FirstName { get; set; }
    }

现在需要将上面的查询数据绑定到这个模型类吗?在不创建 BranchUser 类的情况下,有什么最好的方法吗?因为我的应用程序有另一个具有相同名称的类用户和分支,我觉得为查询数据绑定创建像这样的单独类有些内疚?我说的对吗?

更新:我尝试如下,

var userTree = (from user in users
join location in locations on user.FkHomeLocationId equals location.LocationId
join region in regions on location.RegionId equals region.LocationId
group region by new {
    Regionid = region.LocationId,
    location.LocationId
}
into grp
select new Application.Alerts.Models.AlertUserTreeViewModel() {
    RegionName = regions.FirstOrDefault(s = >s.LocationId == grp.Key.Regionid).LocationName,
    Branchs = new Application.Alerts.Models.Branch() {
        BranchName = locations.FirstOrDefault(b = >b.RegionId == grp.Key.Regionid).LocationName,
        Users = new Application.Alerts.Models.User {
            FirstName = users.Where(u = >u.FkHomeLocationId == grp.Key.LocationId).FirstOrDefault().FirstName
        }
    }
}).ToList();

但是我在这一行遇到编译错误,

Users = new Application.Alerts.Models.User {
    FirstName = users.Where(u = >u.FkHomeLocationId == grp.Key.LocationId).FirstOrDefault().FirstName
}

作为

无法将类型“Alerts.Models.User”隐式转换为“System.Collections.Generic.List

【问题讨论】:

  • 呃..什么?您已经创建了 Branch 和 User 类,那么使用它们吗? select new TreeModel { RegionName = ...
  • @CaiusJard 是的,我试过这样。但问题是Users = users.Where(u = &gt;u.FkHomeLocationId == grp.Key.LocationId).Select(s = &gt;new { FullName = s.FirstName + " " + s.LastName }).ToList()这部分是怎么做的?
  • 你有一个 List 因此使用 userTree .FirstOrDefault() 来获取关于类的模型:将构造函数添加到 TreeModel 和 this.Branchs = new Branch()。顺便说一句,使名称和类型相同不是一个好习惯
  • @PowerMouse 如何将查询数据绑定到模型中?
  • 1.你有一个 TreeModel 对象的列表。 2. 在 select new {... make: select new TreeModel{ RegionName = ...}

标签: c# entity-framework .net-core entity-framework-core


【解决方案1】:

只需向您的匿名类添加额外的 FirstName 属性

var userTree = 
....
select new {
    RegionName = ...
    Branches = new {
        BranchName = ...,
        Users = ....Select(s = >new {
            FirstName=s.FirstName,
            FullName = s.FirstName + " " + s.LastName
        }).ToList()
    }

之后你可以创建视图模型

var treeModel= userTree.Select( new TreeModel {
                RegionName = userTree.RegionName
                Branchs =userTree.Branches.Select(i=> 
                new Branch { 
                 BranchName= i.BranchName,
                 Users= i.Users.Select(j=> new User{ FirstName=j.Firstname})
                )});

如果你只需要你的 userTree 中的全名,你可以转换回来

userTree= userTree.Select( new TreeModel {
                RegionName = userTree.RegionName
                Branchs =userTree.Branches.Select(i=> 
                new Branch { 
                 BranchName= i.BranchName,
                 Users= i.Users.Select( j=> new {j.FullName})
                )});

更新

如果你想使用 dbcontext 运行另一个查询,你可以试试这个

var userTree = (from user in users
join location in locations on user.FkHomeLocationId equals location.LocationId
join region in regions on location.RegionId equals region.LocationId
group region by new {
    Regionid = region.LocationId,
    location.LocationId
}
into grp
select new Application.Alerts.Models.AlertUserTreeViewModel {
    RegionName = regions.FirstOrDefault(s = >s.LocationId == grp.Key.Regionid).LocationName,
    Branches = new new Application.Alerts.Models.Branch {
         BranchId = locations.FirstOrDefault(b = >b.RegionId == 
        grp.Key.Regionid).LocationId,
        BranchName = locations.FirstOrDefault(b = >b.RegionId == grp.Key.Regionid).LocationName,
        Users = users.Where(u = >u.FkHomeLocationId == grp.Key.LocationId).Select(s = >new  User {
             Id=s.Id,
             LastName=s.LastName,
            FirstName= s.FirstName 
        }).ToList()
    }
}).ToList();

【讨论】:

  • 我更新了我的答案。试试看告诉我
  • Cannot implicitly convert type 'System.Collections.Generic.List&lt;&lt;anonymous type: string FirstName&gt;&gt;' to 'System.Collections.Generic.List&lt;Administration.Application.Alerts.Models.User&gt; 显示此错误
  • @Rooter 再次抱歉。请改用新用户 {}。我更新了我的答案。由于是 2 个不同的用户,因此令人困惑。
  • 如果我将 Branch 类的附加属性添加为 BranchId 和 LastName,UserId 为用户类,我应该如何更改为这些分配值的代码
  • @Rooter 我更新了我的答案。再次检查。
【解决方案2】:

您的Branch 有一个List&lt;User&gt;,但您试图实例化为User,这就是您收到错误的原因。使用下面的代码。

Users = users.Where(u = >u.FkHomeLocationId == grp.Key.LocationId).Select(u => new User { FirstName = u.FirstName }).ToList()

【讨论】:

  • @如何获取姓氏并将其分配给全名属性?
  • @Rooter 请检查更新的代码。您可以使用 Select() 方法中的 lambda 表达式分配任何属性。
  • 如果我将 Branch 类的附加属性添加为 BranchId 和 LastName,UserId 为用户类,我应该如何更改为这些分配值的代码
  • 在只是添加更多的属性。即新用户 { FirstName = u.FirstName, LastName = u.LastName, UserId= u.UserId }
猜你喜欢
  • 2010-10-02
  • 2021-09-14
  • 2013-02-08
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-07
相关资源
最近更新 更多