2016-09-06 108 views
0

我正在创建一个web api。我有两个表从2个表中获取详细信息asp.net web api

Employee_details

​​

Id_details

SlNo  R_ID   L_ID 
1   AC1    L001 
2   AC1    L002 
3   AC1    L003 
4   AC2    L004 
5   AC2    L005 

我创建了RESTful服务的web API。我需要获取特定Employeeid的所有L_ID。例如,如果我申请employeeid 2022,我应该得到L001 L002和L003。请帮帮我。我首先使用的代码和使迁移

代码

Model类

Employee_details.cs

public class Employee_details 
{ 
    public int slNo {get; set;} 
    public string Employeeid {get; set;} 
    public string R_ID {get; set;} 

    //Navigation 
    public Id_details Id_details{get; set;} 
} 

Id_details.cs

public class Id_details 
{ 
    public int slNo {get; set;} 
    public string R_ID {get; set;} 
    public string L_ID {get; set;} 

} 

控制器

创建的表
public IQueryable<Employee_id> Getdetails(string employeeid) 
    { 
     return db.Employee_details 
      .Where(b => b.Employeeid.Equals(employeeid, StringComparison.OrdinalIgnoreCase)).Include(c => c.Id_details); 

    } 

由于表2中的R_ID字段不是主键,所以我没有创建外键引用。

+0

添加你的代码你试过了吗? –

+0

简单使用SQL连接将解决你的问题,你为什么不试一试? –

+0

使用实体框架? – Dumisani

回答

0

如果您使用Native SQL,则此查询是您的返回数据。

SELECT ID.L_ID  
     FROM Id_details AS ID 
    INNER JOIN Employee_details ED 
     ON ID.R_ID = ED.R_ID 
    WHERE ED.Employeeid = '2022' 

,或者如果您使用EntitiyFramework

该查询返回equels R_ID记录然后设置Employee_details条件

var res = db.Id_details.Join(db.Employee_details, x => x.R_ID , y => y.R_ID , (x, y) => x).ToList(); 

,或者你可以在containsList选择任何R_ID

var conditionList =db.Employee_details.where(p=>p.Employeeid.equals('2022')).select(p=>p.R_ID); 

然后设置条件使用conditionList搜索

db.Id_details.where(p=> conditionList.contains(p.R_ID)).select (...);