这种从SQL到LINQ的转换是否正确?

时间:2013-09-15 09:55:06

标签: c# sql sql-server linq entity-framework

以下是简单的SQL查询

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

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

我目前正在尝试学习LINQ

所以,我试图将上面的SQL Query转换为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; }
    }
}

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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 ;

答案 2 :(得分: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();