【发布时间】:2021-04-10 13:17:32
【问题描述】:
这个 SQL 在 Linq 中会是什么样子?
你可以看到我要使用的 SQL 语句,我对 Linq 还很陌生:
SELECT user_id
FROM profiles
LEFT JOIN interests ON profiles.id = interests.profile_id
WHERE interest IN ('Shopping', 'Art')
AND Sex IN ('Man', 'Woman')
AND user_id NOT IN (SELECT user_1 FROM matches)
AND user_id != 84
GROUP BY user_id
ORDER BY COUNT(user_id) DESC;
下面你可以看到不同的模型,所有的模型都有一个来自modelbase的id
轮廓模型:
namespace Sparks.Presentation.Entities
{
public partial class Profile: ModelBase
{
public Profile()
{
Interests = new HashSet<Interest>();
}
public int UserId { get; set; }
public string Name { get; set; }
public string Picture { get; set; }
public string Birthdate { get; set; }
public string Sex { get; set; }
public string Orientation { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Interest> Interests { get; set; }
[NotMapped]
public IFormFile FormFile { set; get; }
}
}
兴趣模型:
namespace Sparks.Presentation.Entities
{
public partial class Interest: ModelBase
{
public int ProfileId { get; set; }
public string InterestName { get; set; }
public virtual Profile Profile { get; set; }
}
}
匹配模型:
namespace Sparks.Presentation.Entities
{
public partial class Match: ModelBase
{
public Match()
{
Chats = new HashSet<Chat>();
}
public int User1 { get; set; }
public int User2 { get; set; }
public bool Matchstatus { get; set; }
public bool Matchmade { get; set; }
public DateTime CreatedOn { get; set; }
public virtual User User1Navigation { get; set; }
public virtual User User2Navigation { get; set; }
public virtual ICollection<Chat> Chats { get; set; }
}
}
希望有人能帮帮我吗?
【问题讨论】:
-
也许我的SQL to LINQ Recipe 可以帮助你。
-
你用的是什么ORM工具?
-
“我怎样才能让这个工作代码变得更慢更难维护?”这实际上是同一个问题;)