score:12
Like this:
string delimitedString =
string.Join(",", tasks.Select(
l => string.Format("{0}#{1}", l.TaskID, l.IsComplete)));
If you're using C#-6:
string delimitedString =
string.Join(",", tasks.Select(
l => $"{l.TaskID}#{l.IsComplete}"));
score:2
I think what you need is to implement a ToString for the Task, so it displays what you want.
If you can't (it isnt your code), implement a separate function:
private string StringifyTask(Task task)
{
return string.Format("{0}#{1}", task.TaskId, task.IsComplete);
}
And then use StringifyTask as the argument of Select. Also - note you don't actually need the ToList before the Linq statement.
score:5
You could do it the way Yuval Itzchakov proposed, but I'd rather implement a new method in Task
that you can call - just to keep it easier to read.
public class Task
{
...
public String ToDelimitedString()
{
return String.Format("{0}#{1}", TaskId, IsComplete);
}
}
And then call it like this:
var delimString = String.Join(",", tasks.Select(t => t.ToDelimitedString()));
This way you have your format in one place and can call it from anywhere without inconsistencies.
By the way: I'd also try to find a different name for the class to avoid any possible confusion with System.Threading.Tasks.Task.
Source: stackoverflow.com
Related Query
- Using String.Join for multiple items in a List
- Join together all items of a list in an output string in .NET
- How to Remove multiple items in List using RemoveAll on condition?
- How to reuse a linq expression for 'Where' when using multiple source tables
- Using Linq to group by multiple columns in a list and mark all duplicate items
- IN query using LINQ for IEnumerable List of items
- Join three list using multiple columns c# linq lambda
- How to search a list of objects for a specific string attribute using LINQ in C#
- How to search any items of a list within a string using Linq
- Find indexes in String using multiple search items and one single iteration
- Join a string property which is in a List of objects using Linq
- How to convert a list of anonymous object to string array using linq for return using Json Result in ASP.NET MVC
- c# - Using LinQ to look for duplicate items in a list and updating object properties if so
- Search contains in a string column for a list of strings using LINQ
- How can I find the first items in a list with a different value for a property using Linq?
- Filter List using linq for remove duplicates items
- List or Array of String Contain specific word in Html Source Code
- Using C# 4.8: Most efficient way to remove items matching string patterns from a string array or string list
- How to change property for all items in list using expressions
- How to add multiple items in a list dynamically using linq in c#
- How to join on multiple lists only by list index using VB.Net
- Merge multiple list of string to list of object using VB.NET
- Using multiple LINQ statements with into , for the DefaultIfEmpty() of the left outer join not working
- How to get duplicate items from a list using LINQ?
- C# - code to order by a property using the property name as a string
- Is there a LINQ function for getting the longest string in a list of strings?
- compare two list and return not matching items using linq
- Using LINQ to convert a list to a CSV string
- linq to sql join on multiple columns using lambda
- Can I join a table to a list using linq?
More Query from same tag
- Dynamic LINQ Group By Query in ASP.NET MVC
- What is the event handler equivilent to the LINQ code used here
- Nullable Int in LINQ with outer join
- Split into KeyValuePair instead of Array
- Is there a C# function that checks if a list is empty and doesn't count null as an element?
- Linq and Cross Products
- Why is an instance of indexing a one-dimensional array a BinaryExpression and not a MethodCallExpression?
- How do I group by List<int> in a Dictionary<string, List<int>> where the lists are identical?
- Update field IEnumerable regarding duplicates c# EF
- Check if a key exists in a NewtonSoft JObject C#
- Not able to view the latest file from a directory in ASP.NET
- Enumerable and deferred execution of a method
- Find differences in datatables with linq when datatables have different columns
- How do I generate an html string from group by
- Linq to SQL connections
- Why can't the compiler infer the type for this select call?
- Insert record into db using linq2sql/datacontext
- c# linq if item is in list and get matching items from list
- Using "from in" in a LinqSpecs specification?
- Incorrect count in ASP.NET View
- to avoid reading an rss node that is not present in the feed
- XML File Update
- How to multiply a column under a certain condition by using linq?
- How to choose columns from a query and display in datagrid
- Linq-to-Xml, get element with a specific attribute
- LINQ get parent with most children
- Array.BinarySearch where a certain condition is met
- ASP.NET Core - Get tuple with two values
- LINQ create generic List of nestet objects
- Inserting data into different tables at the same time with Linq-to-SQL