2009-11-17 14 views
41

मैं एक साधारण लैम्ब्डा अभिव्यक्ति है कि कुछ इस तरह चला जाता है है: अबएकाधिक कहाँ लैम्ब्डा में खंड भाव

x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty) 

, अगर मैं एक और जोड़ना चाहते हैं जहां अभिव्यक्ति के लिए खंड, l.InternalName != String.Empty तो क्या होगा कहते हैं, अभिव्यक्ति हो?

+1

यह एक छोटे से विषय से हटकर लेकिन है स्ट्रिंग क्लास में एक विधि है स्ट्रिंग। ISNullOrEmpty जिसे आप स्ट्रिंग के खिलाफ तुलना करने के बजाय उपयोग कर सकते हैं। –

उत्तर

87

x => x.Lists.Include(l => l.Title) 
    .Where(l => l.Title != String.Empty && l.InternalName != String.Empty) 

या

x => x.Lists.Include(l => l.Title) 
    .Where(l => l.Title != String.Empty) 
    .Where(l => l.InternalName != String.Empty) 

हो सकता है आप Where कार्यान्वयन पर देख रहे हैं, तो आप इसे देख सकते हैं एक Func(T, bool) स्वीकार करता है; इसका मतलब है कि:

  • T अपने IEnumerable प्रकार
  • bool मतलब है कि यह एक बूलियन मान

तो वापस जाने के लिए की जरूरत है, जब आप कर

.Where(l => l.InternalName != String.Empty) 
// ^     ^---------- boolean part 
//  |------------------------------ "T" part 
+6

+1 को बूलियन/टी टिप्पणी के लिए –

2
x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty).Where(l => l.Internal NAme != String.Empty) 

या

x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty && l.Internal NAme != String.Empty) 
3

शायद

x=> x.Lists.Include(l => l.Title) 
    .Where(l => l.Title != string.Empty) 
    .Where(l => l.InternalName != string.Empty) 

?

आप शायद यह भी एक ही में रख सकते हैं, जहां खंड: एक ही में

x=> x.Lists.Include(l => l.Title) 
    .Where(l => l.Title != string.Empty && l.InternalName != string.Empty) 
5

आप इसे शामिल कर सकते हैं जहां & & ऑपरेटर के साथ बयान ...

x=> x.Lists.Include(l => l.Title).Where(l=>l.Title != String.Empty 
    && l.InternalName != String.Empty) 

आप उपयोग कर सकते हैं तुलनात्मक ऑपरेटरों में से कोई भी (इसके बारे में सोचें जैसे कि कथन कर रहा है) जैसे ...

List<Int32> nums = new List<int>(); 

nums.Add(3); 
nums.Add(10); 
nums.Add(5); 

var results = nums.Where(x => x == 3 || x == 10); 

... वापस लाना होगा 3 और 10

11

लैम्ब्डा आप Where के पास किसी भी सामान्य सी # कोड शामिल कर सकते हैं, उदाहरण के लिए && ऑपरेटर:

.Where(l => l.Title != string.Empty && l.InternalName != string.Empty) 
संबंधित मुद्दे