C# web API linq to SQL 列表对象
C# web API linq to SQL List object
我正在尝试为自动完成文本框创建一个网络 API。我有 API 处理示例数据,但我不知道如何从我的 table.
添加数据
这是我的控制器:
namespace IMDSEbs.Controllers
{
public class CompanyController : ApiController
{
// GET: api/Company/GetCompanyNames
IMDSDataContext dc = new IMDSDataContext();
public List<CompanyName> results = new List<CompanyName>
{
new CompanyName{ID = 1, Name = "Sonu Nigam"},
};
// GET api/values
public IEnumerable<CompanyName> GetCompanyNames(string query)
{
return results.Where(m => m.Name.Contains(query)).ToList();
}
}
}
这里是table结构体(名称其实是公司名):
ID Name
------------------------------------
1 Abc Company
2 cde Company
3 fgh Company
4 ijk Company
如果你想调用你的数据库?我假设您的上下文是基于 EntityFramework 的,如果是这样您可以这样做:
dc.Companies.Where(x => x.Name.Contains(query)).Select(x => new {x.id, x.Name}).ToList();
确保在抓取数据时调用 where,否则您将抓取所有公司并在内存中过滤它们,这是一个额外的步骤。
这就是我要找的答案。
namespace IMDSEbs.Controllers
{
public class CompanyController : ApiController
{
// GET api/values
public IEnumerable<CompanyName> GetCompanyNames(string query)
{
IMDSDataContext dc = new IMDSDataContext();
List<CompanyName> results = new List<CompanyName>();
foreach(IMDSEbs.Models.Company comp in dc.Companies)
{
results.Add(new CompanyName()
{
ID = comp.CompanyID,
Name = comp.CompanyName
});
}
return results.Where(m => m.Name.Contains(query)).ToList();
}
}
}
要摆脱 foreach,请执行以下操作(可能需要一些修饰...)
return
//Get the Entities you need...
dc.Companies
//Narrow down what you are selecting...
.Where(m => m.Name.Contains(query))
//convert the entity into the object you need...
.Select(comp => new CompanyName(){
ID = comp.CompanyID,
Name = comp.CompanyName})
.ToList();
我正在尝试为自动完成文本框创建一个网络 API。我有 API 处理示例数据,但我不知道如何从我的 table.
添加数据这是我的控制器:
namespace IMDSEbs.Controllers
{
public class CompanyController : ApiController
{
// GET: api/Company/GetCompanyNames
IMDSDataContext dc = new IMDSDataContext();
public List<CompanyName> results = new List<CompanyName>
{
new CompanyName{ID = 1, Name = "Sonu Nigam"},
};
// GET api/values
public IEnumerable<CompanyName> GetCompanyNames(string query)
{
return results.Where(m => m.Name.Contains(query)).ToList();
}
}
}
这里是table结构体(名称其实是公司名):
ID Name
------------------------------------
1 Abc Company
2 cde Company
3 fgh Company
4 ijk Company
如果你想调用你的数据库?我假设您的上下文是基于 EntityFramework 的,如果是这样您可以这样做:
dc.Companies.Where(x => x.Name.Contains(query)).Select(x => new {x.id, x.Name}).ToList();
确保在抓取数据时调用 where,否则您将抓取所有公司并在内存中过滤它们,这是一个额外的步骤。
这就是我要找的答案。
namespace IMDSEbs.Controllers
{
public class CompanyController : ApiController
{
// GET api/values
public IEnumerable<CompanyName> GetCompanyNames(string query)
{
IMDSDataContext dc = new IMDSDataContext();
List<CompanyName> results = new List<CompanyName>();
foreach(IMDSEbs.Models.Company comp in dc.Companies)
{
results.Add(new CompanyName()
{
ID = comp.CompanyID,
Name = comp.CompanyName
});
}
return results.Where(m => m.Name.Contains(query)).ToList();
}
}
}
要摆脱 foreach,请执行以下操作(可能需要一些修饰...)
return
//Get the Entities you need...
dc.Companies
//Narrow down what you are selecting...
.Where(m => m.Name.Contains(query))
//convert the entity into the object you need...
.Select(comp => new CompanyName(){
ID = comp.CompanyID,
Name = comp.CompanyName})
.ToList();