LINQ का उपयोग करके सभी वस्तुओं को एक संग्रह में अपडेट करें


499

वहाँ LINQ का उपयोग कर निम्नलिखित करने के लिए एक रास्ता है?

foreach (var c in collection)
{
    c.PropertyToSet = value;
}

स्पष्ट करने के लिए, मैं एक संग्रह में प्रत्येक वस्तु के माध्यम से पुनरावृति करना चाहता हूं और फिर प्रत्येक वस्तु पर एक संपत्ति का अद्यतन करना चाहता हूं।

मेरा उपयोग मामला यह है कि मेरे पास एक ब्लॉग पोस्ट पर टिप्पणियों का एक गुच्छा है, और मैं एक ब्लॉग पोस्ट पर प्रत्येक टिप्पणी के माध्यम से पुनरावृति करना चाहता हूं और ब्लॉग पोस्ट पर डेटाटाइम को +10 घंटे पर सेट करना चाहता हूं। मैं इसे एसक्यूएल में कर सकता था, लेकिन मैं इसे बिजनेस लेयर में रखना चाहता हूं।


14
दिलचस्प सवाल। व्यक्तिगत रूप से मैं पसंद करता हूं कि आपने इसे ऊपर कैसे प्राप्त किया है - अभी तक जो भी चल रहा है वह स्पष्ट है!
noelicus

8
मैं यहां उसी प्रश्न का उत्तर ढूंढने के लिए आया था, और यह तय किया कि यह भविष्य के डेवलपर्स के लिए सिर्फ उतना ही आसान, कम कोड और समझने में आसान है, जितना आप ओपी में करते हैं, ठीक उसी तरह से करने के लिए।
केसी क्रुक्स्टन

4
आप इसे LINQ में क्यों करना चाहेंगे?
कलस्टर

13
यह प्रश्न गलत चीज़ के लिए पूछता है, एकमात्र सही उत्तर है: डेटा स्रोत को संशोधित करने के लिए LINQ का उपयोग न करें
टिम शाल्टर

मैं इस प्रश्न को ऑफ-टॉपिक के रूप में बंद करने के लिए मतदान कर रहा हूं क्योंकि इस प्रश्न के लगभग सभी उत्तर LINQ के नए प्रोग्रामर्स की समझ के लिए सक्रिय रूप से हानिकारक हैं।
तनवीर बदर

जवाबों:


841

जब आप एक ForEachविस्तार विधि का उपयोग कर सकते हैं , यदि आप केवल उस ढांचे का उपयोग करना चाहते हैं जो आप कर सकते हैं

collection.Select(c => {c.PropertyToSet = value; return c;}).ToList();

ToListआदेश के कारण तुरंत चयन का मूल्यांकन करने की जरूरत है आलसी मूल्यांकन


6
मैंने इसे अपडाउन किया क्योंकि यह एक बहुत अच्छा समाधान है ... एकमात्र कारण मुझे एक्सटेंशन पद्धति पसंद है, यह यह समझने के लिए थोड़ा स्पष्ट करता है कि क्या चल रहा है ... हालांकि आपका समाधान अभी भी बहुत प्यारा है
लोमैक्स

9
यदि संग्रह एक ObservableCollectionकहावत थी, तो एक नई सूची बनाने के बजाय जगह में आइटम बदलना उपयोगी हो सकता है।
कैमरून मैकफारलैंड

7
@desaivv हाँ, यह एक वाक्यविन्यास दुरुपयोग का एक सा है, इसलिए Resharper आपको इस बारे में चेतावनी दे रहा है।
कैमरन मैकफारलैंड

46
IMHO, यह एक साधारण फॉर्च्यून लूप की तुलना में बहुत कम अभिव्यंजक है। ToList () भ्रामक है क्योंकि इसका उपयोग किसी भी चीज़ के लिए नहीं किया जाता है, बल्कि मूल्यांकन के लिए मजबूर किया जाता है जो अन्यथा स्थगित कर दिया जाएगा। प्रक्षेपण भी भ्रामक है क्योंकि इसका उपयोग इसके इच्छित उद्देश्य के लिए नहीं किया गया है; बल्कि, इसका उपयोग संग्रह के तत्वों पर पुनरावृति करने के लिए किया जाता है और किसी संपत्ति तक पहुंच की अनुमति देता है ताकि इसे अपडेट किया जा सके। मेरे दिमाग में एकमात्र सवाल यह होगा कि क्या फ़ॉरच लूप Parallel.ForEach का उपयोग करके समानता से लाभ उठा सकता है या नहीं, लेकिन यह एक अलग सवाल है।
फिलिप

37
यह उत्तर सबसे खराब अभ्यास है। ऐसा कभी न करें।
एरिक लिपर्ट

351
collection.ToList().ForEach(c => c.PropertyToSet = value);

36
@ सन्तोषकुमार: उपयोगcollection.ToList().ForEach(c => { c.Property1ToSet = value1; c.Property2ToSet = value2; });
anth Г И

@CameronMacFarland: निश्चित रूप से यह नहीं होगा क्योंकि संरचनाएं अपरिवर्तनीय हैं। लेकिन अगर आप वास्तव में चाहते हैं, तो आप ऐसा कर सकते हैं:collection.ToList().ForEach(c => { collection[collection.IndexOf(c)] = new <struct type>() { <propertyToSet> = value, <propertyToRetain> = c.Property2Retain }; });
І Г И

11
नई सूची बनाने के बजाय सूची को अद्यतन करने के लिए कैमरन मैकफारलैंड के उत्तर पर इसका लाभ है।
साइमन टेवेसी

7
वाह, यह जवाब वास्तव में उपयोगी नहीं है। सिर्फ लूप का उपयोग करने में सक्षम होने के लिए एक नया संग्रह बनाना
टिम श्मेल्टर

@SimonTewsi चूंकि यह वस्तुओं का एक संग्रह है, सूची को किसी भी स्थान पर अद्यतन किया जाना चाहिए। संग्रह नया होगा, लेकिन संग्रह में वस्तुएं समान होंगी।
क्रिस

70

मैं यह कर रहा हूं

Collection.All(c => { c.needsChange = value; return true; });

मुझे लगता है कि यह ऐसा करने का सबसे साफ तरीका है।
wcm

31
यह दृष्टिकोण निश्चित रूप से काम करता है लेकिन यह All()विस्तार विधि के इरादे का उल्लंघन करता है , जिससे संभावित भ्रम पैदा होता है जब कोई अन्य व्यक्ति कोड पढ़ता है।
टॉम बैक्सटर

यह दृष्टिकोण बेहतर है। प्रत्येक लूप का उपयोग करने के बजाय सभी का उपयोग करना
UJS

2
निश्चित रूप से ToList () को कॉल करने पर इसे प्राथमिकता दें, भले ही यह सभी के लिए इसका उपयोग करने के बारे में थोड़ा भ्रामक हो।
चक्र

1
यदि आप इस तरह एक संग्रह का उपयोग कर रहे हैं, तो List<>इसे ForEach()पूरा करने के लिए विधि बहुत कम गूढ़ तरीका है। पूर्वForEach(c => { c.needsChange = value; })
दान Is नगण्य द्वारा Firelight

27

मुझे वास्तव में एक एक्सटेंशन विधि मिली जो मैं अच्छी तरह से करना चाहता हूं

public static IEnumerable<T> ForEach<T>(
    this IEnumerable<T> source,
    Action<T> act)
{
    foreach (T element in source) act(element);
    return source;
}

4
अच्छा :) लोमैक्स, शायद एक उदाहरण जोड़ते हैं ताकि झांकें इसे 'एक्शन' (बूम टीश!) में देख सकें।
प्योर.क्रोम

2
यह एकमात्र उपयोगी दृष्टिकोण है यदि आप वास्तव में एक foreachलूप (जो भी कारण के लिए) से बचना चाहते हैं ।
टिम श्मेल्टर

@ रेंगो जिसे आप अभी भी टाल नहीं रहे हैं foreachक्योंकि कोड में ही foreachलूप है
गोल्डबिशप

@GoldBishop यकीन है, विधि लूप छुपाता है।
टिम श्मेल्टर

1
लिंक टूट गया है, यह अब यहां उपलब्ध है: codewrecks.com/blog/index.php/2008/08/13/… । वहाँ भी एक ब्लॉग टिप्पणी है कि stackoverflow.com/questions/200574 के लिए लिंक है । बदले में, शीर्ष प्रश्न blogs.msdn.microsoft.com/ericlippert/2009/05/18/… से लिंक करता है । शायद जवाब MSDN का उपयोग करके फिर से लिखा जाएगा (यदि आप चाहते हैं तो भी आप पहले लिंक को क्रेडिट कर सकते हैं)। सिडेनोट: रस्ट में समान विशेषताएं हैं, और अंत में दिए गए और समान फ़ंक्शन को जोड़ा गया: stackoverflow.com/a/50224248/799204
sourcejedi

14

उपयोग:

ListOfStuff.Where(w => w.Thing == value).ToList().ForEach(f => f.OtherThing = vauleForNewOtherThing);

मुझे यकीन नहीं है कि यह लाइनक्यू पर अति प्रयोग कर रहा है या नहीं, लेकिन इसने मेरे लिए काम किया है जब एक विशिष्ट स्थिति के लिए सूची में एक विशेष आइटम को अपडेट करना चाहता है।


7

ऐसा करने के लिए कोई अंतर्निहित एक्सटेंशन विधि नहीं है। हालांकि एक को परिभाषित करना काफी सीधे आगे है। पोस्ट के नीचे एक विधि है जिसे मैंने Iterate कहा जाता है। इसका उपयोग ऐसे किया जा सकता है

collection.Iterate(c => { c.PropertyToSet = value;} );

Iterate स्रोत

public static void Iterate<T>(this IEnumerable<T> enumerable, Action<T> callback)
{
    if (enumerable == null)
    {
        throw new ArgumentNullException("enumerable");
    }

    IterateHelper(enumerable, (x, i) => callback(x));
}

public static void Iterate<T>(this IEnumerable<T> enumerable, Action<T,int> callback)
{
    if (enumerable == null)
    {
        throw new ArgumentNullException("enumerable");
    }

    IterateHelper(enumerable, callback);
}

private static void IterateHelper<T>(this IEnumerable<T> enumerable, Action<T,int> callback)
{
    int count = 0;
    foreach (var cur in enumerable)
    {
        callback(cur, count);
        count++;
    }
}

क्या गणना आवश्यक है, क्या गणना, सम, औसत या अन्य मौजूदा विस्तार विधि के साथ गलत है जो स्केलर मान लौटाता है?
एंथनीवजोन

2
यह जो मैं चाहता हूँ, लेकिन बहुत कम के करीब है .. शामिल मैंने जिस ब्लॉग पोस्ट को पोस्ट किया है उसका कार्यान्वयन समान है लेकिन कोड की कुछ पंक्तियों के साथ।
लोमैक्स

1
IterateHelper ओवरकिल लगता है। एक अधिभार जो एक सूचकांक नहीं लेता है वह बहुत अधिक अतिरिक्त काम कर रहा है (लैम्बडा को कॉलबैक परिवर्तित करें जो सूचकांक लेता है, एक गिनती रखें जो कभी उपयोग नहीं होती है)। मैं समझता हूं कि यह पुन: उपयोग है, लेकिन यह वैसे भी केवल एक फ़ोरलूप का उपयोग करने के लिए एक समाधान है, इसलिए यह कुशल होना चाहिए।
कैमरून मैकफारलैंड

2
@Cameron, IterateHelper 2 उद्देश्य प्रदान करता है। 1) एकल कार्यान्वयन और 2) कॉल समय बनाम उपयोग के दौरान ArgumentNullException को फेंकने की अनुमति देता है। C # पुनरावृत्तियों को निष्पादित करने में देरी हो रही है, सहायक के अजीब व्यवहार को पुनरावृत्ति के दौरान फेंके जाने के विषम व्यवहार को रोकता है।
जारेदपार

2
@JaredPar: इसके अलावा आप एक पुनरावृत्ति का उपयोग नहीं कर रहे हैं। कोई उपज बयान नहीं है।
कैमरन मैकफारलैंड

7

यद्यपि आपने विशेष रूप से एक LINQ समाधान के लिए कहा था और यह प्रश्न काफी पुराना है, मैं एक गैर-LINQ-समाधान पोस्ट करता हूं। ऐसा इसलिए है क्योंकि LINQ (= भाषा एकीकृत क्वेरी ) का उपयोग संग्रह पर प्रश्नों के लिए किया जाना है। सभी LINQ- तरीके अंतर्निहित संग्रह को संशोधित नहीं करते हैं, वे सिर्फ एक नया लौटाते हैं (या एक नए संग्रह के लिए अधिक सटीक पुनरावृत्त)। इस प्रकार आप जो कुछ भी करते हैं जैसे aSelect अंतर्निहित संग्रह को प्रभावित नहीं करता है, आपको बस एक नया मिलता है।

बेशक आप सकता है एक साथ यह कर ForEach(जो LINQ नहीं, वैसे, लेकिन पर एक विस्तार है List<T>)। लेकिन यह सचमुच का उपयोग करता foreachहै, लेकिन एक भेड़ का बच्चा अभिव्यक्ति के साथ। इसके अलावा हर LINQ विधि आंतरिक रूप का उपयोग करके अपने संग्रह जैसे iterates foreachया for, फिर भी यह केवल ग्राहक से यह छुपाता है। मैं इसे किसी भी अधिक पठनीय और अनुरक्षित नहीं मानता (लैम्बडा-एक्सप्रेशंस युक्त विधि को डीबग करते समय अपने कोड को संपादित करने के बारे में सोचें)।

कहा कि यह आपके संग्रह में आइटम को संशोधित करने के लिए LINQ का उपयोग नहीं करना चाहिए । एक बेहतर तरीका यह है कि आप अपने प्रश्न में पहले से ही समाधान प्रदान करें। एक क्लासिक लूप के साथ आप आसानी से अपने संग्रह को पुन: व्यवस्थित कर सकते हैं और इसके आइटम अपडेट कर सकते हैं। वास्तव में उन सभी समाधानों पर भरोसा करना List.ForEachअलग नहीं है, लेकिन मेरे दृष्टिकोण से पढ़ना बहुत कठिन है।

इसलिए आपको उन मामलों में LINQ का उपयोग नहीं करना चाहिए जहाँ आप अपने संग्रह के तत्वों को अद्यतन करना चाहते हैं ।


3
ऑफ-टॉपिक: मैं सहमत हूं, और लिनक्यू के कई उदाहरणों का दुरुपयोग किया जा रहा है, "हाई परफॉर्मेंस लिनक्यू चेन" का अनुरोध करने वाले लोगों के उदाहरण हैं, यह करने के लिए कि एक लूप के साथ क्या पूरा किया जा सकता है, आदि। मैं आभारी हूं कि लिनक्यू नहीं है। मेरे अंदर भी प्रवेश कर गया है, और आमतौर पर इसका उपयोग नहीं करते हैं। मैं देख रहा हूँ कि लोग एक ही क्रिया को करने के लिए LINQ चेन का उपयोग करते हैं, यह महसूस करते हुए कि हर बार LINQ कमांड का उपयोग करने पर आप for"हुड के नीचे" एक और लूप बना रहे हैं । मुझे लगता है कि यह सरल कार्यों को करने के लिए कम वर्बोज़ तरीके बनाने के लिए सिंथेटिक चीनी है, मानक कोडिंग के लिए प्रतिस्थापन नहीं होना चाहिए।
फॉरएवरजेर0

6

मैंने इस पर कुछ बदलाव करने की कोशिश की है, और मैं इस आदमी के समाधान पर वापस जा रहा हूं।

http://www.hookedonlinq.com/UpdateOperator.ashx

फिर, यह किसी और का समाधान है। लेकिन मैंने कोड को एक छोटे पुस्तकालय में संकलित किया है, और इसे नियमित रूप से उपयोग करता है।

मैं उसका कोड यहाँ पेस्ट करने जा रहा हूँ, इस अवसर के लिए कि उसकी साइट (ब्लॉग) भविष्य में किसी बिंदु पर मौजूद है। ("यहाँ आपको सटीक उत्तर की आवश्यकता है", क्लिक, और डेड URL कहते हुए पोस्ट देखने से बुरा कुछ नहीं है।)

    public static class UpdateExtensions {

    public delegate void Func<TArg0>(TArg0 element);

    /// <summary>
    /// Executes an Update statement block on all elements in an IEnumerable<T> sequence.
    /// </summary>
    /// <typeparam name="TSource">The source element type.</typeparam>
    /// <param name="source">The source sequence.</param>
    /// <param name="update">The update statement to execute for each element.</param>
    /// <returns>The numer of records affected.</returns>
    public static int Update<TSource>(this IEnumerable<TSource> source, Func<TSource> update)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (update == null) throw new ArgumentNullException("update");
        if (typeof(TSource).IsValueType)
            throw new NotSupportedException("value type elements are not supported by update.");

        int count = 0;
        foreach (TSource element in source)
        {
            update(element);
            count++;
        }
        return count;
    }
}



int count = drawingObjects
        .Where(d => d.IsSelected && d.Color == Colors.Blue)
        .Update(e => { e.Color = Color.Red; e.Selected = false; } );

1
आप Action<TSource>एक अतिरिक्त प्रतिनिधि बनाने के बजाय उपयोग कर सकते हैं । यह लिखने के समय के रूप में उपलब्ध नहीं हो सकता है, हालांकि।
फ्रैंक जे

हाँ, वह उस समय पुराना स्कूल डॉटनेट था। अच्छी टिप्पणी फ्रैंक।
ग्रानैडकोडर

URL मर चुका है! (यह डोमेन नाम समाप्त हो गया है) अच्छी बात है कि मैंने यहाँ कोड की नकल की है! #patOnShoulder
GranadaCoder

1
मूल्य प्रकार के लिए जाँच करना समझ में आता है, लेकिन शायद where T: structसंकलन समय पर इसे पकड़ने के लिए एक बाधा का उपयोग करना बेहतर होगा ।
ग्रू

4

नहीं, LINQ बड़े पैमाने पर अपडेट करने के तरीके का समर्थन नहीं करता है। एक्सटेंशन पद्धति का उपयोग करने का एकमात्र छोटा तरीका होगा ForEach- IEnumerable पर कोई फ़ॉरएक् एक्सटेंशन पद्धति क्यों नहीं है?


3

मैंने उस के साथ मदद करने के लिए कुछ विस्तार विधियाँ लिखीं।

namespace System.Linq
{
    /// <summary>
    /// Class to hold extension methods to Linq.
    /// </summary>
    public static class LinqExtensions
    {
        /// <summary>
        /// Changes all elements of IEnumerable by the change function
        /// </summary>
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
        /// <param name="change">The way you want to change the stuff</param>
        /// <returns>An IEnumerable with all changes applied</returns>
        public static IEnumerable<T> Change<T>(this IEnumerable<T> enumerable, Func<T, T> change  )
        {
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
            ArgumentCheck.IsNullorWhiteSpace(change, "change");

            foreach (var item in enumerable)
            {
                yield return change(item);
            }
        }

        /// <summary>
        /// Changes all elements of IEnumerable by the change function, that fullfill the where function
        /// </summary>
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
        /// <param name="change">The way you want to change the stuff</param>
        /// <param name="where">The function to check where changes should be made</param>
        /// <returns>
        /// An IEnumerable with all changes applied
        /// </returns>
        public static IEnumerable<T> ChangeWhere<T>(this IEnumerable<T> enumerable, 
                                                    Func<T, T> change,
                                                    Func<T, bool> @where)
        {
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
            ArgumentCheck.IsNullorWhiteSpace(change, "change");
            ArgumentCheck.IsNullorWhiteSpace(@where, "where");

            foreach (var item in enumerable)
            {
                if (@where(item))
                {
                    yield return change(item);
                }
                else
                {
                    yield return item;
                }
            }
        }

        /// <summary>
        /// Changes all elements of IEnumerable by the change function that do not fullfill the except function
        /// </summary>
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
        /// <param name="change">The way you want to change the stuff</param>
        /// <param name="where">The function to check where changes should not be made</param>
        /// <returns>
        /// An IEnumerable with all changes applied
        /// </returns>
        public static IEnumerable<T> ChangeExcept<T>(this IEnumerable<T> enumerable,
                                                     Func<T, T> change,
                                                     Func<T, bool> @where)
        {
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
            ArgumentCheck.IsNullorWhiteSpace(change, "change");
            ArgumentCheck.IsNullorWhiteSpace(@where, "where");

            foreach (var item in enumerable)
            {
                if (!@where(item))
                {
                    yield return change(item);
                }
                else
                {
                    yield return item;
                }
            }
        }

        /// <summary>
        /// Update all elements of IEnumerable by the update function (only works with reference types)
        /// </summary>
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
        /// <param name="update">The way you want to change the stuff</param>
        /// <returns>
        /// The same enumerable you passed in
        /// </returns>
        public static IEnumerable<T> Update<T>(this IEnumerable<T> enumerable,
                                               Action<T> update) where T : class
        {
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
            ArgumentCheck.IsNullorWhiteSpace(update, "update");
            foreach (var item in enumerable)
            {
                update(item);
            }
            return enumerable;
        }

        /// <summary>
        /// Update all elements of IEnumerable by the update function (only works with reference types)
        /// where the where function returns true
        /// </summary>
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
        /// <param name="update">The way you want to change the stuff</param>
        /// <param name="where">The function to check where updates should be made</param>
        /// <returns>
        /// The same enumerable you passed in
        /// </returns>
        public static IEnumerable<T> UpdateWhere<T>(this IEnumerable<T> enumerable,
                                               Action<T> update, Func<T, bool> where) where T : class
        {
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
            ArgumentCheck.IsNullorWhiteSpace(update, "update");
            foreach (var item in enumerable)
            {
                if (where(item))
                {
                    update(item);
                }
            }
            return enumerable;
        }

        /// <summary>
        /// Update all elements of IEnumerable by the update function (only works with reference types)
        /// Except the elements from the where function
        /// </summary>
        /// <param name="enumerable">The enumerable where you want to change stuff</param>
        /// <param name="update">The way you want to change the stuff</param>
        /// <param name="where">The function to check where changes should not be made</param>
        /// <returns>
        /// The same enumerable you passed in
        /// </returns>
        public static IEnumerable<T> UpdateExcept<T>(this IEnumerable<T> enumerable,
                                               Action<T> update, Func<T, bool> where) where T : class
        {
            ArgumentCheck.IsNullorWhiteSpace(enumerable, "enumerable");
            ArgumentCheck.IsNullorWhiteSpace(update, "update");

            foreach (var item in enumerable)
            {
                if (!where(item))
                {
                    update(item);
                }
            }
            return enumerable;
        }
    }
}

मैं इसे इस तरह उपयोग कर रहा हूं:

        List<int> exampleList = new List<int>()
            {
                1, 2 , 3
            };

        //2 , 3 , 4
        var updated1 = exampleList.Change(x => x + 1);

        //10, 2, 3
        var updated2 = exampleList
            .ChangeWhere(   changeItem => changeItem * 10,          // change you want to make
                            conditionItem => conditionItem < 2);    // where you want to make the change

        //1, 0, 0
        var updated3 = exampleList
            .ChangeExcept(changeItem => 0,                          //Change elements to 0
                          conditionItem => conditionItem == 1);     //everywhere but where element is 1

संदर्भ के लिए तर्क की जाँच करें:

/// <summary>
/// Class for doing argument checks
/// </summary>
public static class ArgumentCheck
{


    /// <summary>
    /// Checks if a value is string or any other object if it is string
    /// it checks for nullorwhitespace otherwhise it checks for null only
    /// </summary>
    /// <typeparam name="T">Type of the item you want to check</typeparam>
    /// <param name="item">The item you want to check</param>
    /// <param name="nameOfTheArgument">Name of the argument</param>
    public static void IsNullorWhiteSpace<T>(T item, string nameOfTheArgument = "")
    {

        Type type = typeof(T);
        if (type == typeof(string) ||
            type == typeof(String))
        {
            if (string.IsNullOrWhiteSpace(item as string))
            {
                throw new ArgumentException(nameOfTheArgument + " is null or Whitespace");
            }
        }
        else
        {
            if (item == null)
            {
                throw new ArgumentException(nameOfTheArgument + " is null");
            }
        }

    }
}

2

मेरे 2 पैसे: -

 collection.Count(v => (v.PropertyToUpdate = newValue) == null);

7
मुझे सोच पसंद है, लेकिन यह वास्तव में स्पष्ट नहीं है कि कोड क्या कर रहा है
lomaxx

2

आप अपने संग्रह को एक सरणी में बदलने के लिए LINQ का उपयोग कर सकते हैं और फिर Array.ForEach () का आह्वान कर सकते हैं:

Array.ForEach(MyCollection.ToArray(), item=>item.DoSomeStuff());

जाहिर है कि यह संरचना या इनबिल्ट प्रकार के संग्रह जैसे पूर्णांक या तार के साथ काम नहीं करेगा।


1

यहाँ विस्तार विधि मैं उपयोग कर रहा हूँ ...

    /// <summary>
    /// Executes an Update statement block on all elements in an  IEnumerable of T
    /// sequence.
    /// </summary>
    /// <typeparam name="TSource">The source element type.</typeparam>
    /// <param name="source">The source sequence.</param>
    /// <param name="action">The action method to execute for each element.</param>
    /// <returns>The number of records affected.</returns>
    public static int Update<TSource>(this IEnumerable<TSource> source, Func<TSource> action)
    {
        if (source == null) throw new ArgumentNullException("source");
        if (action == null) throw new ArgumentNullException("action");
        if (typeof (TSource).IsValueType)
            throw new NotSupportedException("value type elements are not supported by update.");

        var count = 0;
        foreach (var element in source)
        {
            action(element);
            count++;
        }
        return count;
    }

क्यों "मूल्य प्रकार के तत्व अद्यतन द्वारा समर्थित नहीं हैं" ?? कुछ भी हस्तक्षेप नहीं करता है कि!
abatishchev

उस परियोजना के लिए विशिष्ट था जिस पर मैं काम कर रहा था। मुझे लगता है कि यह ज्यादातर मामलों में मायने नहीं रखेगा। हाल ही में मैंने इसे फिर से शुरू किया है और इसका नाम बदलकर रन (...) किया है, मान प्रकार की चीज़ को हटा दिया है और इसे शून्य में वापस करने के लिए बदल दिया है और गिनती कोड को गिरा दिया है।
बिल फोनी

List<T>.ForEachयह भी कमोबेश जो कुछ करता है, वह सब के लिए है IEnumerable
हिमब्रोबेरे जूल

अब इस पर पीछे मुड़कर कहूंगा कि मैं सिर्फ एक फॉर्च्यूनर लूप का उपयोग करूंगा। इस तरह से कुछ का उपयोग करने का एकमात्र लाभ यह है कि यदि आप तरीकों को एक साथ चेन करना चाहते हैं और क्रिया को निष्पादित करने के बाद चेन को जारी रखने के लिए फ़ंक्शन से गणना करने योग्य वापस कर सकते हैं। अन्यथा यह बिना किसी लाभ के सिर्फ एक अतिरिक्त विधि कॉल है।
बिल फॉर्नी

0

मुझे लगता है कि आप एक क्वेरी के अंदर मूल्यों को बदलना चाहते हैं ताकि आप इसके लिए एक फ़ंक्शन लिख सकें

void DoStuff()
{
    Func<string, Foo, bool> test = (y, x) => { x.Bar = y; return true; };
    List<Foo> mylist = new List<Foo>();
    var v = from x in mylist
            where test("value", x)
            select x;
}

class Foo
{
    string Bar { get; set; }
}

लेकिन यह नहीं है कि आप का क्या मतलब है।


यह सही दिशा की तरह चल रहा है जिसके लिए v की गणना करने के लिए कुछ की आवश्यकता होगी, अन्यथा यह कुछ नहीं करेगा।
एंथनीवजोन

0

आप LINQ के लिए बैच ऑपरेशन फ्रेमवर्क Magiq का उपयोग कर सकते हैं ।


-3

मान लें कि हमारे पास नीचे जैसा डेटा है,

var items = new List<string>({"123", "456", "789"});
// Like 123 value get updated to 123ABC ..

और अगर हम सूची को संशोधित करना चाहते हैं और सूची के मौजूदा मूल्यों को संशोधित मूल्यों से बदलना चाहते हैं, तो पहले एक नई खाली सूची बनाएं, फिर प्रत्येक सूची आइटम पर संशोधित पद्धति को लागू करके डेटा सूची के माध्यम से लूप करें,

var modifiedItemsList = new List<string>();

items.ForEach(i => {
  var modifiedValue = ModifyingMethod(i);
  modifiedItemsList.Add(items.AsEnumerable().Where(w => w == i).Select(x => modifiedValue).ToList().FirstOrDefault()?.ToString()) 
});
// assign back the modified list
items = modifiedItemsList;

2
आप कुछ ऐसा क्यों बनायेंगे जो O (n) रनटाइम एक्जीक्यूट ओ (n ^ 2) में चला सकता है या इससे भी बदतर हो सकता है? IM इस बात से अनजान है कि linq का काम कैसे होता है लेकिन मैं देख सकता हूँ कि यह n समस्या के लिए न्यूनतम ^ 2 समाधान है।
फॉलनराइपर
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.