我该如何编写这个嵌套查询?

发布于 2024-10-22 17:46:44 字数 859 浏览 1 评论 0原文

我正在努力编写一个查询,该查询取决于两个具有自己的查询操作的表的结果。

谁能帮助我吗?


我的解决方法如下:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜光 2024-10-29 17:46:44

由于 MessageReciever 是从与 Message 相同的数据上下文加载的,因此我们可以将它们连接到同一个查询中。

public IQueryable<Message> GetMessages(int user_id)
{    
    // Get MessageReceiver tables that share receiver id
    var messageReceivers = GetMessageReceivers().Where(messageReceiver => messageReceiver.receiver_id == user_id);

    // get all messages that have been recieved by a user
    var messages = from m in DataContext.Messages
                   join r in messageReceivers
                   on m.id equals r.message_id
                   select m;                  

    //return the messages
    return messages;
}

这将导致类似于以下内容的 SQL:(假设您将 LINQ to SQL 或 LINQ to EF 与 Microsoft SQL 提供程序一起使用)

SELECT [t0].[id], ... other columns ...
FROM [Messages] AS [t0]
INNER JOIN [MessageReceivers] AS [t1] ON [t0].[id] = [t1].[message_id]

Seeing as the MessageRecievers are loaded from the same data context as the Messages we can join them in the same query.

public IQueryable<Message> GetMessages(int user_id)
{    
    // Get MessageReceiver tables that share receiver id
    var messageReceivers = GetMessageReceivers().Where(messageReceiver => messageReceiver.receiver_id == user_id);

    // get all messages that have been recieved by a user
    var messages = from m in DataContext.Messages
                   join r in messageReceivers
                   on m.id equals r.message_id
                   select m;                  

    //return the messages
    return messages;
}

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)

SELECT [t0].[id], ... other columns ...
FROM [Messages] AS [t0]
INNER JOIN [MessageReceivers] AS [t1] ON [t0].[id] = [t1].[message_id]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文