Using C# Lambda to Split String and Search Value

Using C# Lambda to split string and search value

Using a Linq pipeline and anonymous objects:

"0:12211,90:33221,23:09011"
.Split(',')
.Select(x => x.Split(':'))
.Select(x => new { employeeId = x[0], payrollId = x[1] })
.Where(x=> x.employeeId == "23")

Results in this:

{
employeeId = "23",
payrollId = "09011"
}

These three lines represent your data processing and projection logic:

.Split(',')
.Select(x => x.Split(':'))
.Select(x => new { employeeId = x[0], payrollId = x[1] })

Then you can add any filtering logic with Where after this the second Select

Split a string of an array with a lambda stream using linq

If the names in your string will always be the "first name" first then the "last name", then you can do something like this:

string[] result = fullNames.Select(s => s.Split(' '))
.Select(s => $"{s[1].ToUpper()} {s[0]}")
.ToArray();

Split, for-loop with replace to lambda expression

Try something like this:

q.ACBValue.Split(',')
.Select(s => new
{
Search = "value='" + s + "' ",
Replace = "value='" + s + "' checked=\"checked\""
})
.ToList()
ForEach(tuple =>
qACBOption = q.ACBOption.Replace(tuple.Search, tuple.Replace));

This solution uses ForEach method of the List<T> class. To make it visible, you have to convert previous sequence to list by explicitly calling extension method ToList.

There is another approach, in which you are aggregating the results along the way using the Aggregate extension method:

q.ACBOption =
q.ACBValue.Split(',')
.Select(s => new
{
Search = "value='" + s + "' ",
Replace = "value='" + s + "' checked=\"checked\""
})
.Aggregate(
q.ACBOption,
(res, tuple) => res.Replace(tuple.Search, tuple.Replace));

How to flatten the result of a nested split in a C# Lambda expression

You were very close. The method to flatten a sequence of sequences is SelectMany. We could add one to the end of your existing code, but since it already ends with a Select, we can in fact just change it to a SelectMany and we're done:

List<KeyValue> mc = "a:b;c:d,e;f:g,h,i"
.Split(';')
.SelectMany(a =>
{
int colon = a.IndexOf(':');
string left = a.Substring(0, colon);
string right = a.Substring(colon + 1);
List<KeyValue> result = right.Split(',')
.Select(x => new KeyValue { Key = left, Value = x }).ToList();
return result;
})
.ToList();

C# lambda Expression In where clause

you should use contain. because split returns string array.

 s.jobOperaitngsys.Split(',').Contains(student.studentknowOS)

C# Lambda expression to pull all names by comma separated ids

You can do something like this:

var roleIds ="1,3,5,9".Split(',').Select(s => int.Parse(s));

var roleNames = db.Roles.Where(k => roleIds.Contains(k.Id)).Select(p => p.Role);


Related Topics



Leave a reply



Submit