Thanks JM,
Here is what I have come up with. Yet to be tested:
Please check it for meCode:Public Function GetMessagesByUserID(ByVal userid As Integer) As List(Of MessageWithRecipient) Dim result As New List(Of MessageWithRecipient)() Using con As New MySqlConnection(strCon) Dim cmd As MySqlCommand = New MySqlCommand("SELECT r.MessageRecipientID, r.MessageID,r.MessageRecipientTypeID,r.UserID,r.CreatedDate,r.MessageFolderID,r.MessageStatusTypeID, " & _ "m.SentByUserID,m.Subject,m.Body,m.CreatedDate As `MessageCreatedDate`,m.MessageTypeID, " & _ "u.UserID FROM MessageRecipients r JOIN messages m ON r.MessageID = m.MessageID JOIN users u ON m.SentByUserID = u.UserID WHERE(r.UserID = ?UserID) ORDER BY m.CreatedDate DESC", con) cmd.Parameters.AddWithValue("?UserID", userid) con.Open() Dim reader As MySqlDataReader = cmd.ExecuteReader While reader.Read result.Add(New MessageWithRecipient(Membership.GetUser(reader.GetInt32("UserID")), _ New Message(reader.GetInt32("MessageID"), reader.GetInt32("SentByUserID"), reader.GetString("Subject"), reader.GetString("Body"), reader.GetDateTime("MessageCreatedDate"), reader.GetInt32("MessageTypeID")), _ New MessageRecipient(reader.GetInt32("MessageRecipientID"), reader.GetInt32("MessageID"), reader.GetInt32("MessageRecipientTypeID"), reader.GetInt32("UserID"), reader.GetDateTime("CreatedDate"), reader.GetInt32("MessageFolderID"), reader.GetInt32("MessageStatusTypeID")))) End While con.Close() reader.Close() cmd.Dispose() End Using Return result.ToList End Function
I really don't understand the LINQ query in the example. I know their MessagesWithRecipient class returns three objects of type Acount,Message and MessageRecipient respectively.
Then their LINQ returns a list of those objects. What I don't understand is how a gets cast to type Account, m to type Message, r to type MessageRecipient in the following code:Code:public class MessageWithRecipient { public Account Sender { get; set; } public Message Message { get; set; } public MessageRecipient MessageRecipient{ get; set; } }
Code:IEnumerable<MessageWithRecipient> messages = (from r in dc.MessageRecipients join m in dc.Messages on r.MessageID equals m.MessageID join a in dc.Accounts on m.SentByAccountID equals a.AccountID where r.AccountID == AccountID && r.MessageFolderID == (int)Folder orderby m.CreateDate descending select new MessageWithRecipient() { Sender = a, Message = m, MessageRecipient = r }).Skip((PageNumber - 1)*10).Take(10);





Reply With Quote