我该如何编写这个嵌套查询?
我正在努力编写一个查询,该查询取决于两个具有自己的查询操作的表的结果。
谁能帮助我吗?
我的解决方法如下:
public IQueryable<Message> GetMessages(int user_id)
{
// Get MessageReceiver tables that share receiver id
IQueryable<MessageReceiver> messageReceivers = GetMessageReceivers().Where(messageReceiver => messageReceiver.receiver_id == user_id);
List<Message> messages = new List<Message>();
foreach (MessageReceiver messageReceiver in messageReceivers)
{
foreach (Message message in DataContext.Messages)
{
if (message.id == messageReceiver.message_id)
{
messages.Add(message);
}
}
}
return messages.AsQueryable<Message>();
}
问候, 斯科特·尼姆罗德
I am struggling to write a query that depends on the results of two tables that have their own query operations.
Can anyone help me?
My work around is the following:
public IQueryable<Message> GetMessages(int user_id)
{
// Get MessageReceiver tables that share receiver id
IQueryable<MessageReceiver> messageReceivers = GetMessageReceivers().Where(messageReceiver => messageReceiver.receiver_id == user_id);
List<Message> messages = new List<Message>();
foreach (MessageReceiver messageReceiver in messageReceivers)
{
foreach (Message message in DataContext.Messages)
{
if (message.id == messageReceiver.message_id)
{
messages.Add(message);
}
}
}
return messages.AsQueryable<Message>();
}
Regards,
Scott Nimrod
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 MessageReciever 是从与 Message 相同的数据上下文加载的,因此我们可以将它们连接到同一个查询中。
这将导致类似于以下内容的 SQL:(假设您将 LINQ to SQL 或 LINQ to EF 与 Microsoft SQL 提供程序一起使用)
Seeing as the MessageRecievers are loaded from the same data context as the Messages we can join them in the same query.
This will result in SQL similar to the following: (assuming that you are using LINQ to SQL or LINQ to EF with a Microsoft SQL provider)