【问题标题】:Random choose of row in table随机选择表中的行
【发布时间】:2022-01-23 11:32:53
【问题描述】:

我在 Xamarin.Forms(C#) 中从我的数据库中加载了一个表,该表有 x 行。每行有3个参数(类型是in()-Id(int),firstword(string),secondword(string)。我需要从这个表/列表中生成一个随机行,并将所有3个参数获取到新的int(id ) 一个字符串 (string fw, sw)。感谢您的帮助。

using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
     conn.CreateTable<Post>();
     var posts = conn.Table<Post>().ToList();
     
     Random r = new Random();
     int i = r.Next(posts.Count);

     int id = ???;
     string fw = ???;
     string sw = ???;
}

【问题讨论】:

    标签: c# list xamarin.forms


    【解决方案1】:

    posts 是List,只需获取列表的第 n 个元素

    var post = posts[i];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 2021-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      相关资源
      最近更新 更多