Linq,EF Core-按一个字段分组,然后根据其他字段从其他表中获取数据列表

时间:2019-04-26 01:15:45

标签: linq entity-framework-core

我正在尝试从Linq获取特定格式的输出。 这个问题已改写-

SQL视图-SomeView

    Id T_Id
    4   2 
    6   5
    6   7

SQL表-用户

    T_Id  fname lname
    2     mary   smith
    5     john    pope
    7     steve   blair

SomeView是QueryType,我使用DbQuery进行了映射。

    public class SomeView
    {
       public int Id {get; private set;}
       public int T_Id { get; private set; }

       public User User { get; set; } //can't navigate here
    }

    public class User
    {
       public int T_Id { get; set; }
       public string fname {get; set;}
       public string lname{get; set;}

       public SomeView SomeView{ get; set; } //can't navigate here
    }

数据库中没有定义外键约束,因为SomeView是SQL视图。 您不能将Navigation与QueryType一起使用。因此无法在User和SomeView之间进行映射,或者我不知道该怎么做。

     public class SomeViewModel
     {
        public int Id { get;  set; }
        public List Users{get; set;}
     }

finally my linq in progress - 

    from t in SomeView
    group new {t} by t.Id into grp
    select new SomeViewModel{
       Id = grp.Key,
       Users = grp.Select(x => x.t.User).ToList()
       //need help here to get Users based on T_Id
      }

最终API数据输出应采用以下格式。

[{
  "Id" : "4",
  "users":[{
         "T_Id": 2,
         "fname": "mary",
         "lname": "smith"
      }]
},
{
  "Id" : "6",
  "users":[{
         "T_Id": 5,
         "fname": "john",
         "lname": "pope"
      },
      {
         "T_Id": 7,
         "fname": "steve",
         "lname": "blair"
      }
      ]
}]

2 个答案:

答案 0 :(得分:0)

首先,我认为您可以使用任何序列化程序将对象转换为所需的格式。

var serialized = JsonConvert.SerializeObject(data)

第二,回到您的问题,这是代码。但是,您需要在变量周围添加“,并摆脱为可读性而添加的字符串连接。此外,此代码是如此特定于您的问题,对于更通用的解决方案,请采用第一种方法。

var mainData = string.Join(',', data.Select(x => $" {{ {nameof(x.Id)} :  {x.Id}, " +
                                                           $"{nameof(User)}s: " +
                                                           $"[ {string.Join(',', x.Users.Select(y => $"{{ {nameof(User.T_Id)} : {y.T_Id} }}"))}]" +
                                                           $"}}"));
var result = $"[{mainData}]" ;

当您更改问题时,我更新了答案。因此,您需要首先加入someView和用户以使他们在一起,然后再按someView.id分组。这是代码

        var someViewsUsersJoin = someViews.Join(users, l => l.t_id, r => r.t_id, (someView, user) => new {someView, user});

        var result = someViewsUsersJoin.GroupBy(x => x.someView.id).Select(x => new SomeViewModel()
        {
            Id = x.Key,
            Users = x.Select(y => y.user).ToList()
        });

答案 1 :(得分:0)

最终以这种方式解决-

 public class SomeView
{
   public int Id {get; private set;}
   public int T_Id { get; private set; }

}

public class User
{
   public int T_Id { get; set; }
   public string fname {get; set;}
   public string lname{get; set;}
}

public class SomeViewModel
{
   public int Id { get;  set; }
   public List<User> Users{get; set;}
}

   from t in SomeView
   join u in User on v.T_Id equals u.T_Id
   group new {t, u} by t.Id into grp
   select new SomeViewModel{
      Id = grp.Key,
      Users = grp.Select(x => x.u).ToList()
  }

那很简单:)