Usinq Linq to get rows based on comma-separated value [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have comma-separated data in my column called col1, and I have an array of strings
IEnumerable<string> year = {"1990","1991","1992","1993","1994","1995","1996","1997","1998","1999","2000"}
I have tried the following
searchfrom = searchfrom.Where(x => years.Contains(x.col1.Replace(',', ' '))).ToList();
and
searchfrom = searchfrom.Where(x => years.Contains(x.col1)).ToList();
I want that row which is match any "year" into "col1"

To optimize such queries, you should first convert your years collection into a hash set.
var years = new HashSet<string>(new [] { "1990", "1991", ... });
In your Where clause, you need to split the contents of each of your records, x, for which you can use x.Split(','). Then, you need to check whether any of these subparts are contained within the years collection.
var result = searchfrom.Where(x =>
x.Split(',').Any(years.Contains)
).ToList();

Related

Get text patters from string using regular expression [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
In C# .NET
I have a string with
PSL44T-VK5VF-2B
PSL-44TVK5-VF2B
PS-L44-TVK-5VF-2B
using regular expression can I get the result format as
XXXXXX-XXXX-XX <- PSL44T-VK5VF-2B
PSL-XXXXXX-XXXX <- PSL-44TVK5-VF2B
XX-XXX-XXX-XXX-2B <- PS-L44-TVK-5VF-2B
XX-L44-XXX-XXX-XX <- PS-L44-TVK-5VF-2B
Your question is not clear. But if you want to filter a list of strings with regular expressions, you can do as follows:
List<string> inputs = new List<string>();
inputs.Add("PSL44T-VK5VF-2B");
inputs.Add("PSL-44TVK5-VF2B");
inputs.Add("PS-L44-TVK-5VF-2B");
var myRegex = new Regex("^PSL-.{6}-.{4}$"); // for PSL-XXXXXX-XXXX
List<string> resultList = inputs.Where(f => myRegex.IsMatch(f)).ToList();
and result:

How to find the name of the person from the list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How to return a person's name when we search for a hobby and it should return empty when there are no matching hobbies.
There are many ways to skin this cat. Using Linq is probably the most standard.
The operation you want to do is to select the Key of the dictionary where the value (the array of hobbies) contains the string "Yoga"
val keys = hobbies.Where(keyvalue => keyvalue.contains("Yoga")).Select(keyvalue => keyvalue.key);
now you have a sequence of keys where one of the hobbies is Yoga.
If there is only a single one, and it's an error if there are more, get CC with keys.Single().
If there could be zero or more, and you just want an arbitrary one, you want keys.FirstOrDefault() which returns null if there isn't one.
In between you have SingleOrDefault() for 0 or 1, or First() for 1 or more select an arbitrary one.

How to split and then join comma separated words into a string to create condition? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have given words like:
general, sports, weather
I want to create a push notification condition like:
condition = "'general' in topics || 'sports' in topics || 'weather' in topics",
How can I do this in one -or two- shot in C# without looping? So I can build a new string and I use it like below:
condition = dynamicCondition,
Try this one:
string input = "general, sports, weather";
string output = string.Join(" || ", input.Split(',').Select(s => $"'{s}' in topics"));
Note: don't forget to using System.Linq

How to linq to split string (only if delimer exists) and create array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
public class itemObject { public string items; }
values - item = "name1,name2" or items = "item3"
Need linq to split by ',' if exists else one string array.
This doesnt require linq, its the default behaviour of String.Split
var array = items.Split(',');

String contains a lot of copies, how to get rid of them? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I got something like this:
string s="Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;"
and now I want to get rid of the copies in the string...so that in the end s should be like this:
s="Solid;Gass;Liquid;"
Try this:
var parts = s.Split(';');
var distinctParts = parts.Distinct();
var newString = string.Join(";", distinctParts);
Where:
Split will give you an array with all the words of your string, taking the specified character as the word separator (; in this case).
Distinct will give your a collection with the unique words of your array.
Finally, Join composes a new string with the unique words, using the specified string (;in this case) as the separator.
You can split the string, then find the distinct instances and join them back in one line:
string s = "Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;";
s = string.Join(";", s.Split(';').Distinct());

Categories