score:5
in general linq would be used to query information. in this case you're also querying the info.
what you could do however is the following:
dts.connections.foreach((conman) =>
{
dts.events.fireinformation(0, "", string.format("connection manager {0} has connection string {1}", connman.name, connman.connectionstring), "", 0, ref fireagain);
}
that is, if dts.connections is a list, otherwise you can't do this. (can't tell from your question if it's an ienumerable or a list)
a workaround would be to create an extension method and the use it anyway. like this:
public void foreach<t>(this ienumerable<t> source, action<t> action)
{
foreach(t item in source)
{
action(item);
}
}
after that, you can use the foreach method on your source as shown in the example.
score:2
dts.connections
.foreach(connman => dts.events.fireinformation(0, "", string.format("connection manager {0} has connection string {1}", connman.name, connman.connectionstring), "", 0, ref fireagain));
if connections is not a ienumerable, change first line with ;
dts.connections.tolist()
score:7
i would not suggest trying to convert this to linq, because linq is meant to be used for functional programming, not imperative. if you wanted to compare it to sql, then it would be without dml.
dts.events.fireinformation
doesn't seem to return a value that you could select. it probably fires an event or something, and it also uses a ref
parameter. that piece of code doesn't look like a query at all, and i think it shouldn't be forced into one. a foreach
loop is perfectly fine in this case, as you're actually doing work inside it and not querying data (which is what linq would be for).
if you needed to filter a collection based on some condition, if you need to join separate collections or something like that, then linq might be a better tool for the job.
Source: stackoverflow.com
Related Query
- Convert simple ForEach into a Linq statement
- How do I convert Foreach statement into linq expression?
- Convert simple Left Outer Join and group by SQL statement into Linq
- Can I convert Dictionary loop inside of a foreach loop into a LINQ statement
- How to convert foreach loop with a switch statement into LINQ
- Convert simple SQL group-by into LINQ to SQL
- Convert nested for-loops into single LINQ statement
- Is it possible to convert foreach operation into LINQ if it does two things?
- How to convert this foreach loop into Linq code?
- I want to convert this foreach loop to a LINQ statement
- convert foreach loop to linq code
- Can you convert this Linq statement into Lambda without using join statements?
- Convert piece of code into LINQ (short syntax)
- Convert Lambda into Linq Statement with nested for loop
- How can convert a For statement into LINQ
- convert foreach to LINQ statement using where
- Can I can convert this C# code into some Linq code?
- Convert a sql query with a subquery into a linq statement
- Convert if and foreach statement to select and where in linq
- Convert T-SQL statement into LINQ expression
- Convert the code from Foreach loop to Linq
- Refactor C# foreach Loop into Linq Statement
- How can i convert this code snippet into LINQ format?
- Convert Foreach into linq Expression
- How to convert a foreach statement to LINQ using C#
- Convert Foreach Loop into Linq in C#
- Coverting a foreach loop containing an if statement into linq method syntax in C#
- Convert Sql Statement into Linq for use with C#, Entity Framework, MVC3
- How to refactor Linq Group By and Foreach statement into one Linq Query
- I want to convert the following code in Foreach to Linq
More Query from same tag
- LINQ JOIN ON TOP 1 ROW
- Is it possible to set NLS_SORT using Nhibernate configuration?
- Select top1 with Linq
- how to read nested elements using Linq to xml
- LINQ group by and select random element from each group
- Filter a List of Strings and select only one occurance of a string with multiple occurance
- "Nullable object must have a value" when Where uses a method?
- LINQ aggregate multiple tables
- JSon.net linq join
- Can I split an IEnumerable into two by a boolean criteria without two queries?
- Filtering datatable where sum of grouped column value is not zero
- C# - MongoDB Linq Query failing with embedded document interface
- Getting the previous node of a searched keyword in xml
- How to query related objects with nhibernate using contains
- Search query using .Contains() for parameters that combine two columns
- How can I perform this type of JOIN in Entity framework with LINQ
- Can I use FirstOrDefault() or First() inside Where()
- Selecting a single value using linq if already duplicate values exist in mvc4
- C# Select View from Oracle DB with LINQ
- Why does IEnumerable.Where method allow a predicate that can change the data?
- using LINQ to get the top N percent of a list
- Getting error in LINQ query to sqlite
- How can I retrieve two entities that are associated with a connection entity, only under the name record2roleid?
- Using LINQ with Open XML (Excel)
- Unable to convert SQL Query to LINQ Query for Left Outer Join
- Linq query with multiple count
- Use multiple conditions in join LINQ. i,e AND
- Group the indexes of the same elements in a array in C#
- How to get index using LINQ?
- I can not make the correct linq request