0

下面是简单的 SQL 查询

select RelationshipID from Relationships r
where
r.RequestReceiverID = 3 and
r.RequestSenderID = (select MemberID from Members m
                     where m.Name = 'Cipla')

上面的查询工作正常,给了我预期的结果。

我目前正在尝试学习 LINQ

因此,我尝试将上述 SQL 查询转换为 LINQ

这是我的尝试

var relation = 
    (from relationship in db.RelationShips
     where relationship.RequestReceiverID == LoggedInUserID && 
           relationship.RequestSenderID == 
               (from m in db.Members
                where m.Name == SelectedUserName
                select m.MemberID).FirstOrDefault()
            select relationship).FirstOrDefault();

但是在执行上述查询时,出现以下错误

Invalid column name 'Member_MemberID'.
Invalid column name 'Member_MemberID1'.

编辑

接下来我正在尝试更新数据库中的数据

relation.StatusID = (from p in db.StatusMasters
                     where p.Status == Status
                     select p.StatusID).FirstOrDefault();

db.SaveChanges();

根据 AgentFire 的要求进行了编辑

//Members.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace Munim_File_Sharing.Models
{
    public class Member
    {
        [Key]
        public int MemberID { get; set; }
        public string Name { get; set; }
        public string Password { get; set; }
        public string MobileNo { get; set; }
        public string FileName { get; set; }
        public DateTime MemberSince { get; set; }
        public int MemberTypeID { get; set; }

        public List<Payment> Payments { get; set; }
        public List<Relationship> RelationshipSender { get; set; }
        public List<Relationship> RelationshipReceiver { get; set; }
    }
}
4

3 回答 3

1

获取会员ID

var memberId = (from m in db.Members
                where m.Name == SelectedUserName
                select m.MemberID).FirstOrDefault();

获取关系

var relation = 
    (from r in db.RelationShips
     where r.RequestReceiverID == LoggedInUserID && 
           r.RequestSenderID == memberId
     select r.RelationshipID).FirstOrDefault(); 

你的问题,你试过吗...

var relationID = 
    (from relationship in db.RelationShips
     where relationship.RequestReceiverID == LoggedInUserID && 
           relationship.RequestSenderID == 
               (from m in db.Members
                where m.Name == SelectedUserName
                select m.MemberID).FirstOrDefault()
            select relationship.RelationshipID).FirstOrDefault();//added RelationshipID to the query

基于问题的编辑...

获取会员ID

var memberId = (from m in db.Members
                where m.Name == SelectedUserName
                select m.MemberID).FirstOrDefault();

获取关系

var relation = 
    (from r in db.RelationShips
     where r.RequestReceiverID == LoggedInUserID && 
           r.RequestSenderID == memberId
     select r).FirstOrDefault(); 

更新状态。。

if (relation != null){
  relation.StatusID = (from p in db.StatusMasters
                     where p.Status == Status
                     select p.StatusID).FirstOrDefault();

db.SaveChanges();
}
于 2013-09-15T10:48:52.147 回答
0

你不能写这样的查询我认为你需要这样的东西

var relationID = (from relationship in db.RelationShips
where relationship.RequestReceiverID == LoggedInUserID &&
db.Members.FirstOrDefault(x=>x.Name==SelectedUserName).MemberID == relationship.RequestSenderID
select relationship 
).FirstOrDefault().RelationshipID ;
于 2013-09-15T10:19:19.603 回答
0

再试试这个

var MemberList=from m in db.Members.where(x=>x.m.Name ==    SelectedUserName).select(x=>x.MemberID)).toList();

var relationID = (from relationship in db.RelationShips.where(x=>x.RequestReceiverID == LoggedInUserID && MemberList.contains(x.RequestSenderID))
                   select relationship).FirstOrDefault();
于 2013-09-15T10:23:08.010 回答