2009-06-08 15 views
17

निम्न क्वेरी कैसे दिखाई देगी यदि मैं विस्तार विधि वाक्यविन्यास का उपयोग कर रहा था?linq विधि वाक्यविन्यास (क्वेरी सिंटैक्स नहीं) के साथ GroupBy

var query = from c in checks 
group c by string.Format("{0} - {1}", c.CustomerId, c.CustomerName) 
into customerGroups 
select new { Customer = customerGroups.Key, Payments = customerGroups } 
+0

अपने भविष्य के संदर्भ के लिए, इस प्रश्न का उत्तर सी # 3.0 विनिर्देशन में दिया गया है, जिसे आप इंटरनेट से डाउनलोड कर सकते हैं। सभी क्वेरी परिवर्तन नियमों को निर्दिष्ट किया गया है। –

+0

ty, नहीं पता था कि परिवर्तन नियम दस्तावेज किए गए थे। –

उत्तर

22

यह इस तरह दिखेगा:

var query = checks 
    .GroupBy(c => string.Format("{0} - {1}", c.CustomerId, c.CustomerName)) 
    .Select (g => new { Customer = g.Key, Payments = g }); 
3

के बाद से संकलक आप के लिए इस अनुवाद करता है, Reflector ऊपर आग और एक बार देख ले।

8

सबसे पहले, बुनियादी जवाब:

var query = checks.GroupBy<Customer, string>(delegate (Customer c) { 
    return string.Format("{0} - {1}", c.CustomerId, c.CustomerName); 
}).Select(delegate (IGrouping<string, Customer> customerGroups) { 
    return new { Customer = customerGroups.Key, Payments = customerGroups }; 
}); 

तो, आप कैसे बाहर इन बातों को अपने आप को समझ सकता हूँ?

सबसे पहले, here से प्रतिबिंब डाउनलोड करें, और इसे इंस्टॉल करें।

फिर एक छोटा सा कंसोल प्रोग्राम की तरह एक नमूना प्रोग्राम बनाएं, जिसमें वह कोड शामिल है जिसे आप विश्लेषण करना चाहते हैं। यहाँ कोड मैंने लिखा है:

using System; 
using System.Collections.Generic; 
using System.Linq; 

namespace ConsoleApplication11 
{ 
    public class Customer 
    { 
     public Int32 CustomerId; 
     public Int32 CustomerName; 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      var checks = new List<Customer>(); 
      var query = from c in checks 
         group c by String.Format("{0} - {1}", c.CustomerId, c.CustomerName) 
          into customerGroups 
          select new { Customer = customerGroups.Key, Payments = customerGroups }; 
     } 
    } 
} 

तो फिर तुम कि निर्माण, और खुले परावर्तक, और यह पूछना सवाल में exe फ़ाइल को खोलने के लिए।

फिर आप प्रश्न में विधि पर नेविगेट करते हैं, जो मेरे मामले में ConsoleApplication11.Program.Main था।

यहां की चाल प्रतिबिंबक के विकल्प पृष्ठ पर जाना है, और इसे सी # 2.0 सिंटैक्स दिखाने के लिए कहें, जो उपयुक्त स्थिर विधि कॉल के साथ लिंक को प्रतिस्थापित करेगा। ऐसा करने से मेरा पीछा कोड देता है:

private static void Main(string[] args) 
{ 
    List<Customer> checks = new List<Customer>(); 
    var query = checks.GroupBy<Customer, string>(delegate (Customer c) { 
     return string.Format("{0} - {1}", c.CustomerId, c.CustomerName); 
    }).Select(delegate (IGrouping<string, Customer> customerGroups) { 
     return new { Customer = customerGroups.Key, Payments = customerGroups }; 
    }); 
} 

अब, बेशक इस कोड lambdas और इसी तरह के साथ एक सा खूबसूरत लिखा जा सकता है, क्या @mquandershowed की तरह है, लेकिन परावर्तक के साथ, कम से कम आप को समझने के लिए सक्षम होना चाहिए विधि कॉल शामिल किया जा रहा है।

+0

ReSharper भी क्वेरी वाक्यविन्यास से/रूपांतरण में है। – bzlm

1

मुझे पता है कि यह एक पुराना सवाल है, लेकिन नए पाठकों के लिए, this gitub कोड पर एक नज़र डालें।

यह रोज़लिन को क्वेरी सिंटैक्स लेने और विस्तार विधि वाक्यविन्यास में बदलने के लिए उपयोग करता है।

संबंधित मुद्दे