我想尝试加入下面的3个表是代码

时间:2015-09-22 13:36:44

标签: linq

var result = (from p in db.push_notifications
              join nu in db.notification_recievers on p.id equals nu.push_notification_id
              join nt in db.notification_types on p.notification_type_id equals nt.id
              where (p.id == pushNotificationId && p.send_criteria == criteria && nu.delete_flag == false && p.delete_flag == false && nt.delete_flag == false)
              select new NotificationList
              {
                  conferenceId = p.conference_id,
                  pushNotificationId = p.id,
                  notificationId = nt.id,
                  notificationType = nt.notification_type,
                  nottificationDate = p.created_dt_tm,
                  criteria = (int)p.send_criteria,
                  notificationMessage = p.notification_msg,
                  userEmail=null,
                  userInterests = **getInterestNamesByPushNotificationId(p.id)**,
                  userEvents=null
              }).Distinct();

public string getInterestNamesByPushNotificationId(int id)
{
   string interests = string.Empty;
   var query = from i in db.interests
               join pn in db.notification_recievers
               on i.id equals pn.interest_id
               where pn.push_notification_id == id && pn.delete_flag == false
               select new
               {
                   name = i.name
               };

  foreach (var intr in query.Distinct())
  {
      if (interests == "")
      {
          interests = intr.name;
      }
      else
      {
          interests = interests + ", " + intr.name;
      }
  }
  return interests;
}

这让我犯了错误

  

LINQ to Entities无法识别方法' System.String   getInterestNamesBy PushNotification(Int32)'方法和这种方法   无法翻译成商店表达。

2 个答案:

答案 0 :(得分:0)

实体框架试图在SQL端执行你的LINQ子句,显然从SQL的角度来看,没有相当于'getInterestNamesBy PushNotification(Int32)'。

您需要强制选择Enumerable,然后使用所需方法重新选择对象。

不理想,但这样的事情应该有用 - (没有经过测试,所以很好)。

var result = (from p in db.push_notifications
                      join nu in db.notification_recievers on p.id equals nu.push_notification_id
                      join nt in db.notification_types on p.notification_type_id equals nt.id
                      where (p.id == pushNotificationId && p.send_criteria == criteria && nu.delete_flag == false && p.delete_flag == false && nt.delete_flag == false)
                      select new { p=p, nu = nu, nt = nt }).AsEnumerable().Select( x => new NotificationList()
                      {
                          conferenceId = x.p.conference_id,
                          pushNotificationId = x.p.id,
                          notificationId = x.nt.id,
                          notificationType = x.nt.notification_type,
                          nottificationDate = x.p.created_dt_tm,
                          criteria = (int)x.p.send_criteria,
                          notificationMessage = x.p.notification_msg,
                          userEmail=null,
                          userInterests = getInterestNamesByPushNotificationId(x.p.id),
                          userEvents=null
                      }).Distinct();

答案 1 :(得分:0)

i have done it this way

In my model 
 using (NotificationService nService = new NotificationService())
            {
 modelView = nService.DetailsOfNotifications(pushNotificationId, criteriaId).Select(x => new NotificationViewModelUI(x.conferenceId, x.pushNotificationId, x.notificationId, x.notificationType, x.nottificationDate, x.criteria, x.notificationMessage, x.userEmail, nService.getInterestNamesByPushNotificationId(x.pushNotificationId), nService.getIEventTitlesByPushNotificationId(x.pushNotificationId))).ToList();
}

 public NotificationViewModelUI(int conferenceId, int pushNotificationId, int notificationId, string notificationType, DateTime dateTime, int criteria, string nMessage, string emailId = null, string interestNames = null, string eventTitles = null)
        {
            this.conferenceId = conferenceId;
            this.pushNotificationId = pushNotificationId;
            this.notificationId = notificationId;
            this.notificationType = notificationType;
            this.notificationDate = dateTime;
            this.sendCriteria = (NotificationCriteria)criteria;
            this.notificationMessage = nMessage;
            this.emailId = NotificationCriteria.SpecificUser.Description() +"...  "+ emailId;
            this.interestNames = NotificationCriteria.UserByInterests.Description() + "...  " + interestNames;
            this.eventTitles = NotificationCriteria.UserByEvents.Description() + "...  " + eventTitles;
        }