我在ASP.NET MVC中有一个项目。
我需要连接一个数据库,并从后面使用后缀'r'
从中拉出所有客户端。我试过了:
public class MyDBRepository
{
public IEnumerable<MyClient> GetNamesBySuffix(char symbol)
{
List<MyClient> myClients = new List<MyClient>();
using (MyDBEntities m = new MyDBEntities())
{
List<Client> Clients = new List<Client>();
Clients = m.Clients.Where(c => c.name[c.name.Length-1] == symbol).ToList();
foreach (Client client in Clients)
{
MyClient newClient = new MyClient() { Name = client.name };
myClients.Add(newClient);
}
return myClients;
}
}
}
我收到了System.NotSupportedException
,我无法在sql中使用索引(c.name[c.name.Length-1])
。
帮助者的ty!
答案 0 :(得分:1)
尝试
Clients = m.Clients.Where(c => c.name.EndsWith(symbol.ToString())).ToList();
答案 1 :(得分:0)
查看下面的代码,使用EndsWith根据最后一个字符查找数据。
public class MyDBRepository
{
public IEnumerable<MyClient> GetNamesBySuffix(char symbol)
{
List<MyClient> myClients = new List<MyClient>();
using (MyDBEntities m = new MyDBEntities())
{
myClients = m.Clients.Where(c => c.name.Trim().EndsWith(symbol+""))
.Select(c=>new MyClient {name=c.name})
.ToList();
return myClients;
}
}
}