如何根据具有双向条件的查询/票证计算新消息

时间:2014-05-22 08:47:47

标签: linq linq-to-sql asp.net-4.0

我正在使用crm应用程序。我开发它最后我很困惑我如何检测新消息,因为标志必须有显示和消息不必为真。

这是我的数据库架构,用于理解问题。

enter image description here 这是我构造的查询以获取新的消息计数:

int new_messages = 0;
                foreach (var enquiry in db.Enquiries.Where(i => (i.ForwardTo.Equals(userid) || i.AttendBy.Equals(userid))).ToList())
                {
                    if (enquiry != null)
                    {
                        bool IsIns = true;
                        foreach (var flag in db.Flags.Where(f => f.Enquiry_History_id.Equals(enquiry.Ref_no) && f.User_id.Equals(userid)).Select(f => new { IsDisplay = f.IsDisplay }).ToList())
                        {
                            if (flag != null)
                            {
                                if (flag.IsDisplay == false)
                                {
                                    IsIns = false;
                                }
                            }
                        }
                        if (IsIns == true)
                        {
                            foreach (var message in db.Messages.Where(m => m.Enquiry_History_id.Equals(enquiry.Ref_no) && m.User_id.Equals(userid)).Select(m => new { IsRead = m.IsRead }).ToList())
                            {
                                if (message.IsRead == true)
                                {
                                    //do another stuff
                                }
                                else
                                {
                                    new_messages++;
                                }
                            }
                        }
                    }
                }
                lbl_msg.Text = new_messages.ToString();

这里我必须将每个已经AttendBy或ForwardBy的查询带到用户并首先检查它应该显示不是假的,并且在检查之后根据每条消息不应该将IsRead设置为true。我正以最好的方式尝试,但这并没有让我按照自己的意愿出局。

1 个答案:

答案 0 :(得分:1)

我有一个想法,只需插入两个值为true,生成消息,第二个为false,为froward。并且只发现哪些IsRead为假。

像这样的事情:

using (DataClassesDataContext db = new DataClassesDataContext())
        {
            string userid = db.Users.Where(u => u.Username.Equals((String)Session["Username"])).Select(u => u.Ref_no).SingleOrDefault().ToString();
            bool IsIns = true;
            foreach (var enquiry in db.Enquiries.Where(i => i.AttendBy.Equals(userid) || i.ForwardTo.Equals(userid)).ToList())
            {
                if (enquiry != null)
                {
                    foreach (var flag in db.Flags.Where(f => f.Enquiry_History_id.Equals(enquiry.Ref_no) && f.User_id.Equals(userid)).Select(f => new { IsDisplay = f.IsDisplay }).ToList())
                    {
                        if (flag.IsDisplay == false)
                        {
                            IsIns = false;
                        }
                        if (IsIns == true)
                        {
                            foreach (var message in db.Messages.Where(m => m.Enquiry_History_id.Equals(enquiry.Ref_no)).ToList())
                            {
                                if (message != null)
                                {
                                    bool IsIns1 = false;
                                    foreach (var messageflag in db.MessageFlags.Where(mf => mf.MessageId.Equals(message.Id) && mf.UserId.Equals(userid)).Select(mf => new
                                    {
                                        IsRead = mf.IsRead,
                                        User_id = mf.UserId,
                                        Message = message.body,
                                        CreatedDate = message.Created_date
                                    }).ToList())
                                    {
                                        if (messageflag.IsRead == false)
                                        {
                                            IsIns1 = true;
                                        }
                                        if (IsIns1 == true)
                                        {
                                            DataRow dr = dt.NewRow();
                                            dr["Ref_no"] = enquiry.Ref_no.ToString();
                                            dr["Name"] = db.Users.Where(u => u.Ref_no.Equals(messageflag.User_id)).Select(u => u.FirstName + ' ' + u.LastName).SingleOrDefault().ToString();
                                            dr["UserId"] = db.Users.Where(u => u.Ref_no.Equals(messageflag.User_id)).Select(u => u.Ref_no).SingleOrDefault().ToString();
                                            dr["Message"] = messageflag.Message.ToString();
                                            dr["CreatedDate"] = messageflag.CreatedDate.ToString();
                                            dt.Rows.Add(dr);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            myDataSet.Tables.Add(dt);
            lbl_count_messages.Text = myDataSet.Tables[0].Rows.Count.ToString();
            if (myDataSet.Tables[0].Rows.Count == 0)
            {
                message_alert.Visible = false;
            }
            Repeater_Messages.DataSource = myDataSet;
            Repeater_Messages.DataBind();
        }