score:1
linq is generally used to select and operate on a sub-set of a collection, what you're doing doesn't really fit its purpose.
you could define your foreach loops as lambdas if you really wanted to, but you won't get any special benefit. ie:
action<datacolumn> buildaction = (datacolumn targetcolumn) =>
{
htmlbuilder.append("<td align='left' valign='top'>");
htmlbuilder.append(myrow[targetcolumn.columnname].tostring());
htmlbuilder.append("</td>");
};
targettable.columns.foreach(buildaction);
score:1
i would not suggest using the follow because your code is more efficient. but since you are looking for an example to learn from… you could use the following linq statement to achieve the same results.
var sb = new stringbuilder();
const string rowbegin = "<tr align='left' valign='top'>";
const string rowend = "</tr>";
const string cellbegin = "<td align='left' valign='top'>";
const string cellend = "</td>";
targettable.asenumerable()
.select(row => string.format("{0}{1}{2}",
rowbegin,
string.join(string.empty,
row.table.columns
.cast<datacolumn>()
.select(column => string.format("{0}{1}{2}",
cellbegin,
(row.isnull(column) ? string.empty : row[column].tostring()),
cellend))
.toarray()
),
rowend)
)
.tolist()
.foreach(y => sb.append(y));
score:1
rather than thinking of this as a string, consider using linq to xml to generate the nodes:
var rows = from row in targettable.rows.asenumerable()
select new xelement("tr",
new xattribute("align", "left"),
new xattribute("valign","top"),
from column in targettable.columns.asenumerable()
select new xelement("td",
new xattribute("align", "left"),
new xattribute("valign", "top"),
myrow[targetcolumn.columnname].tostring()
)
);
translating this into lambda syntax:
var rows = targettable.rows.asenumerable().select(row => new xelement("tr",
new xattribute("align", "left"),
new xattribute("valign","top"),
targettable.columns.asenumerable().select(column => new xelement("td",
new xattribute("align", "left"),
new xattribute("valign", "top"),
myrow[targetcolumn.columnname].tostring()
))
));
if you need the result as a string. just call tostring() on rows.
one of the main advantages of thinking of this as xml over using a string builder is that you will properly escape invalid strings like <>&" and ensure valid xhtml as a result.
Source: stackoverflow.com
Related Query
- Is it possible to rewrite the below code in Linq/Lambda approach
- code first approach error: the specified type member 'yyyxx' is not supported in linq to entities
- Is possible to rewrite the code so as to reduce the number of selects?
- How to insert a record in lambda expression and possible way to shorten the length of code
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- What is the Efficiency and Performance of LINQ and Lambda Expression in .Net?
- LINQ Lambda Join Error - cannot be inferred from the usage
- Where to draw the line - is it possible to love LINQ too much?
- Has anyone done the Linq 101 samples with Lambda syntax?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation EF Core 3.1
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to rewrite this LINQ using join with lambda expressions?
- What is the equivalent of XML PATH and Stuff in Linq lambda expression (GROUP_CONCAT/STRING_AGG)?
- The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation
- how does one compare the date part of a nullable datetime in a linq / lambda query?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Left outer join using LINQ -- understanding the code
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- Is it possible to see the SQL query of a LINQ command?
- Is it possible to ignore a list item using the Skip method of the LINQ then apply skip on the list without losing that item?
- Refactor Linq code and "LINQ to Entities does not recognize the method"
- Is there a java lambda expression equivalent of the C# linq let?
- Linq - OrderBy the minimum value of two columns using lambda expressions?
- How can I write the following code more elegantly using LINQ query syntax?
- C# LINQ to group by a list of objects in the format as shown below
- Is there an easy way to append lambdas and reuse the lambda name in order to create my Linq where condition?
- The LINQ expression could not be translated. Eiither rewrite the query in a form that can be translated
More Query from same tag
- How to retrieve data from multiple tables using Entity Framework
- VB.NET Compare 2 String Lists of different lengths for identical matches, lists will have duplicate values and List1 needs everything in List2
- How do I build up dynamic linq queries to solve a set of constraints?
- System.Linq.Dynamic - Select Column Value Issue
- Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible
- List of inherited classes with mixed properties
- Using Expression Tree in LINQ to Entity Framework Core
- Unable to find nested property using LINQ
- MoreLinq ExceptBy with only one matching property
- How to filter entity framework result with multiple columns using a lambda expression
- C# Method to Compare DateTime Fields Specified During Execution?
- How do I use LINQ group by clause to return unique employee rows?
- How to specify generic constraint: class inherited from interface?
- Return Rows with Unique and Duplicate values from dataTable using LINQ
- Selecting Partial Substring in a LINQ
- Linq if list null return null
- NHibernate select top N with condition on children using fetch
- about return type in linq
- Could not find an implementation of the query pattern Error
- Entity Framework query Related Entities in single query
- C# linq query aggregate nullable boolean
- How can I select from a SQL DB using linq and EF where ExpiryDate is 30 days to expire from today using DateDiff function
- Overhead of using linq queries on non-changing list
- LINQ Cannot Implicitily Convert
- List of predicates in Entity Framework Where clause
- Sum of decimal in null list causing exception
- How do I report progress while executing a LINQ expression on a large-ish data set
- Distinct of items in dictionary<T,IEnumerable<T>>
- LINQ Lambda Left join with an Inner join
- ContainsValue VB