1

我想在我的数据库上写一个查询,找到所有长于 4 个字符的用户名并打印它们,我的代码是:

我还需要将其更改为列表/数组的某种返回类型吗?

public string[] FindNameByLength(int minimumCharNumber)
    {

        var query = from u in db.Users
                    where u.FullName.Length > minimumCharNumber
                    select u.FullName;


        string[] namesLength;
        int counter;

        foreach (var s in query)
        {
         namesLength.Concat(new[] {s });
        }
        return namesLength;

    }

如何将每个名称添加到数组中?

4

3 回答 3

2

使用Length属性检查字符串长度。尝试这个 :

 public void FindName()
   {
       var query = from u in db.Users
                   where u.FullName.Length > 4
                   select u.FullName;

       foreach(var s in query) 
           Console.WriteLine(s);
   }
于 2013-02-13T13:38:59.293 回答
1

您只需一行代码(使用 LINQ)就可以做到这一点:

public void FindName()
{
    db.Users.Where(x => x.FullName.Length > 4).ToList().ForEach(x => Console.WriteLine(x.FullName));
}
于 2013-02-13T13:39:43.187 回答
1

写入控制台并将它们作为列表返回。您也可以将字符长度作为参数传递给函数。

public List<string> FindName(int ChrLength)
{
       var query = db.Users.Where(u => u.FullName.Length > ChrLength)
                           .Select(u => u.FullName).ToList();

       foreach(var s in query) 
           Console.WriteLine(s);
       return query;
}
于 2013-02-13T13:42:09.117 回答