簡體   English   中英

使用帶有特定后綴的LINQ(EF)從表中獲取名稱

[英]Get Names from a table, using LINQ (EF) with a specific suffix

我在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])

嘗試

Clients = m.Clients.Where(c => c.name.EndsWith(symbol.ToString())).ToList();

查看下面的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;
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM