可能有人使用lambda将以下linq查询语法转换为方法语法?

时间:2018-05-17 16:23:15

标签: c# linq lambda

IList<Student> studentList = new List<Student>() { 
        new Student() { StudentID = 1, StudentName = "John", Age = 18, StandardID = 1 } ,
            new Student() { StudentID = 2, StudentName = "Steve",  Age = 21, StandardID = 1 } ,
            new Student() { StudentID = 3, StudentName = "Bill",  Age = 18, StandardID = 2 } ,
            new Student() { StudentID = 4, StudentName = "Ram" , Age = 20, StandardID = 2 } ,
            new Student() { StudentID = 5, StudentName = "Ron" , Age = 21 } 
    };

    IList<Standard> standardList = new List<Standard>() { 
        new Standard(){ StandardID = 1, StandardName="Standard 1"},
        new Standard(){ StandardID = 2, StandardName="Standard 2"},
        new Standard(){ StandardID = 3, StandardName="Standard 3"}
    };

    var studentsWithStandard = from stad in standardList
                       join s in studentList
                       on stad.StandardID equals s.StandardID
                       into sg
                           from std_grp in sg 
                           orderby stad.StandardName, std_grp.StudentName 
                           select new { 
                                            StudentName = std_grp.StudentName, 
                                            StandardName = stad.StandardName 
                            };

foreach (var group in studentsWithStandard)
    {
        Console.WriteLine("{0} is in {1}", group.StudentName, group.StandardName);
    }
我试过了。我的代码正在关注。

 var studentsWithStandard  = standardList.GroupJoin(studentList, stand => stand.StandardID, s => s.StandardID,
            (stand, students) => new {StandardName = stand.StandardName, studentGroup = students}).OrderBy(an => an.StandardName);

输出将是这样的:

John is in Standard 1
Steve is in Standard 1
Bill is in Standard 2
Ram is in Standard 2

我是从http://www.tutorialsteacher.com/codeeditor?cid=cs-JUmITE得到的 提前谢谢。

2 个答案:

答案 0 :(得分:2)

根据我的经验,//ADDING NEW ITEM INTO THE ITEM TABLE $(document).on('submit', '#product_form', function(event){ event.preventDefault(); btn_action="add_pricelvl"; //Set variable to call the add new item var valdata = $(this).serialize(); //Array with field value var tax = $('#item_tax').val(); //checkbox tax var taxvalue = $('#item_taxvalue').val(); //inputbox tax var tabledets = it_det //Read the detail table .rows() .data(); var arr1=[]; var i=0; //Put the datatable rows in the array for (i=0; i<tabledets.length; i++){ arr1[i]=tabledets.rows(i).data(); } //call ajax function and send variable to php file. $.ajax({ url:'item_action.php', method:"POST", data:{ btn_action:btn_action, valdata:valdata, tax:tax, taxvalue:taxvalue, arr1:arr1 }, success : function(data) { $('#product_form')[0].reset(); $('#productModal').modal('hide'); $('#alert_action').fadeIn().html('<div class="alert alert-success">'+data+'</div>'); $('#action').attr('disabled', false); $('#item_data').DataTable().ajax.reload(); }, error : function () { $('<div>').html('Found an error!'); } }) }); 是查询语法比lambda语法更易读的地方,但无论如何......

我强烈推荐阅读Jon Skeet的优秀书籍C#In Depth。关于LINQ的章节给出了各种查询语法转换为什么的非常明确的解释。 https://www.manning.com/books/c-sharp-in-depth-third-edition

只有一个join的{​​{1}}表达式将转换为join方法,而不是from。如果Join之前有两个GroupJoin,则会使用GroupJoin

您希望这可以进行内部联接:

from

答案 1 :(得分:0)

您可以像这样翻译查询:

var ans = standardList.Join(studentList, stad => stad.StandardID, s => s.StandardID, (stad, s) => new { stad, s })
                      .OrderBy(stads => stads.stad.StandardName).ThenBy(stads => stads.s.StudentName)
                      .Select(stads => new { stads.s.StudentName, stads.stad.StandardName });

请注意,@ JamesFaix的答案提供了一个更有效的文字版本,它结合了JoinSelect

实际上,您的查询不需要into的查询理解版本:

var studentsWithStandard = from stad in standardList
                           join s in studentList on stad.StandardID equals s.StandardID
                           orderby stad.StandardName, s.StudentName
                           select new {
                               StudentName = s.StudentName,
                               StandardName = stad.StandardName
                           };

请注意,您的查询的严格翻译会涉及GroupJoin / SelectMany,但由于您未尝试进行左连接,因此无需翻译:

var ans2 = standardList.GroupJoin(studentList, stad => stad.StandardID, s => s.StandardID, (stad, sg) => new { stad, sg })
                       .SelectMany(stadsg => stadsg.sg.Select(s => new { stadsg.stad, s }))
                       .OrderBy(stads => stads.stad.StandardName).ThenBy(stads => stads.s.StudentName)
                       .Select(stads => new { stads.s.StudentName, stads.stad.StandardName });