walktewy
V2EX  ›  问与答

golang 数组能否实现类似 .Net Linq 的类似功能

  •  
  •   walktewy · Jul 30, 2018 · 3100 views
    This topic created in 2856 days ago, the information mentioned may be changed or developed.
    从结构体数组中筛选相关要素,排序等功能,就像数组能执行 SQL 一样


    底下为.Net 的代码
    请问除了 https://github.com/ahmetb/go-linq
    还有更好的实现吗


    class Program
    {
    public class Student
    {
    public int StID;
    public string LastName;
    }
    public class CourseStudent
    {
    public string CourseName;
    public int StID;
    }
    static Student[] students=new Student[]{
    new Student{StID=1,LastName="Carson"},
    new Student{StID=2,LastName="Klassen"},
    new Student{StID=3,LastName="Fleming"},
    };
    static CourseStudent[] studentsInCourses=new CourseStudent[]{
    new CourseStudent{CourseName="Art",StID=1},
    new CourseStudent{CourseName="Art",StID=2},
    new CourseStudent{CourseName="History",StID=1},
    new CourseStudent{CourseName="History",StID=3},
    new CourseStudent{CourseName="Physics",StID=3},
    }
    static void Main()
    {
    var query=from s in students
    join c in studentsInCourses on s.StID equals c.STID
    where c.CourseName=="History"
    select.LastName;
    foreach(var q in query)
    {
    Console.WriteLine("Student taking History:{0}",q);
    }
    }
    }
    1 replies    2018-07-31 08:00:10 +08:00
    mmdsun
        1
    mmdsun  
       Jul 31, 2018 via Android
    ReactiveX for go 应该有这种功能。我只用过 rxjava
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3946 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 05:11 · PVG 13:11 · LAX 22:11 · JFK 01:11
    ♥ Do have faith in what you're doing.