replace List.foreach to LINQ

replace List.foreach to LINQ

By : TheGame
Date : November 18 2020, 03:49 PM
should help you out I'm new to LINQ and doing some experiments with it. , here is the original code
code :
table.Columns.Cast<DataColumn>().ToList().ForEach(col => header.Add(col.ColumnName));
DataTable table = new DataTable();
List<string> header = new List<string>();

foreach (DataColumn col in table.Columns)
    if (col.ColumnName.StartsWith("Id")) // you can remove this line if you want to add all of them
    .ForEach(col =>
        if (col.ColumnName.StartsWith("Id"))
var headers = table.Columns.Cast<DataColumn>()
        .Where(col => col.ColumnName.StartsWith("Id"))
        .Select(col => col.ColumnName);


Share : facebook icon twitter icon
How to replace foreach with LINQ expressions

How to replace foreach with LINQ expressions

By : zXcongducXz
Date : March 29 2020, 07:55 AM
This might help you , No, the equivalent LINQ form is
code :
var filteredProducts = products.Where(p =>
    arrayString.Any(s => p.ProblemDesc.Contains(s));
Linq to format a list of items to replace foreach loop

Linq to format a list of items to replace foreach loop

By : praveen raj
Date : March 29 2020, 07:55 AM
Hope this helps I have got a single list of items which has all the comments and reply's to a post. I would like to format it depending on comments and reply's together by comparing CommentID to ReplyToCommentId.
code :
from c in comments
where !c.IsReply
from r in new[] { c }.Concat(
                    comments.Where(r => c.CommentID == r.ReplyToCommentId)
select r
    .Where(c => !c.IsReply)
    .SelectMany(c => new[] { c }.Concat(
                    comments.Where(r => c.CommentID == r.ReplyToCommentId)
LINQ ForEach with Replace

LINQ ForEach with Replace

By : Jesse Byars
Date : March 29 2020, 07:55 AM
I wish this help you The problem is the ToList(). The result is not visible in the variable you use afterwards.
Try out the following:
code :
var list = Query<Client>(sql).ToList();
list.ForEach(l => l.DateOfBirth = l.DateOfBirth.Replace("01/01/1700", "n/a"));
Replace foreach loop with linq

Replace foreach loop with linq

By : Juan Garcia Ramos
Date : March 29 2020, 07:55 AM
will be helpful for those in need I tried to replace code , I think what you need is the ForEach method ;)
code :
mpwrapper.parser.Discoveries.ToList().ForEach(s => { solution.AddFile("Discoveries", s.DisplayStringName + ".mpx", s); });
Replace foreach with linq

Replace foreach with linq

By : hehao
Date : March 29 2020, 07:55 AM
Hope this helps This translates reasonably well. Each of the foreach-es translate into their own from, the few variables are each lets, and all of the ifs end up being where's.
code :
var query = from element in elementList
            from row in dt.Rows.Cast<DataRow>()
            from column in dt.Columns.Cast<DataColumn>()
            let ColumnName = column.ColumnName
            let ColumnData = row[column].ToString()
            let currentElement = Regex.Replace(element.Field_Label, @"\W", "")
            where (element.Module_Field_ID != null && currentElement == ColumnName)
            || (element.Field_Type_Name != "Checkbox" 
                    && "Q_" + element.Column_Name_ID == ColumnName
                || element.Field_Type_Name == "Checkbox"
                    && "Q_" + element.Column_Name_ID + "_" + element.Value_Column_Name_ID == ColumnName)
            select new { Element = element, ColumnData = ColumnData };

foreach (var item in query)
    item.Element.ColumnValue = item.ColumnData;
where IsValid(element, ColumnName, currentElement)
Related Posts Related Posts :
  • Console application doesn't obey Thread.Join
  • System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
  • Creating a scripting environment for a C# program
  • Is it reasonable to replace .Equals with a method rather than override it?
  • How I can capture multiple keys?
  • How to have all my classes default functionalities like .ToString method
  • Business Object properties: model as Objects or Enums?
  • Loading this assembly would produce a different grant set from other instances
  • How to call a method without first calling it's constructor in C#
  • Can't set a date time in one of my entity framework objects, says NullReferenceException
  • GETJob() Win32 Print spooler api 64bit The parameter is incorrect, how to fix?
  • Switch Case Causing Trouble In Going To A particular Function
  • Switch Case Calling A Function Based On The User's Choice
  • Accessing WebBrowser on different Threads
  • CPU Usage Avoid 100% With Environment.ProcessorCount?
  • Select distinct categories from database
  • C# app getting Watson dialog when calling TerminateProcess
  • The underlying provider failed on Open in entity framework connection
  • Compile a library without checking references
  • Can we create and access a registry key in HKEY_LOCAL_MACHINE without running application with admin permissions in C#
  • Control light intensity with a UI slider
  • Threading (Does these threads exits or aborts itself?)
  • Change the function of a button if Logged in
  • how to accept value in date format in textbox in C# windows form?
  • remove the lines from RichtextBox?
  • How to call String.Split that takes string as separator?
  • Specify task timeout in parallel linq to objects
  • comparing two custom objects to prevent duplicates
  • How to Print Text from ComboBox into a MessageBox
  • How can I do a Func<object[],Expression<Func<T,bool>>> dynamic?
  • C# SqlDataReader No data exists for the row/column
  • My code is not giving the desired output
  • Is it possible to have multiple MVC routes point to the same controller/view?
  • Updating Listbox results in " Invalid cross-thread access."
  • Finding Elbow Angle with Kinect
  • Host a mvc web api application in the sub-folder of a website
  • How to generate a sequential unique id in a thread-safe way
  • How to add a separator to a WPF combobox that is databound?
  • optimize linq query with related entities
  • Optimize this code for large input
  • Is there any way to clear all list box short way?
  • ACR122 Device Programming sample does not find reader
  • Parallel Library: does a delay on one degree of parallelism delay all of them?
  • How to convert a var which contains Long to an Long[]
  • Quickly prune and create valid data combinations
  • Get 3 parameters out of a Dictionary<string, Dictionary<string Action>>
  • Regex IsMatch taking too long to execute
  • Shuffling divs using C# behindcode
  • ASP.NET Project and IE10 Compatibility Mode issues in Windows 7
  • Why can't I loop through this dictionary?
  • RavenDB Stream for Unbounded Results - Connection Resilience
  • How to remove the focus border of a CheckBox in C# Visual Studio?
  • Exception while using String.Format "Index (zero based) must be greater than or equal to zero and less than the siz
  • Caching an LDAP connection
  • C# How to detect kill event sender
  • How to Display Splashscreen and login form at same time using c#
  • Read .sql script with parameters
  • Why would I need to compile/generate methods/code at runtime?
  • Unable to create a constant value of type 'Project.Models.Subcategory'. Only primitive types or enumeration types are su
  • How to run possibly synchronous or possibly asynchronous code?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org