I'm new to linq. I have a string having the following format
code:description;code2:description2;code3:description3... etc.
Records are separated with ; character and each record has 2 fields separated with : character.
I'm writing a linq query to extract a list of objects having as fields code and description.
I have written the following query which seems to produce correct results, but I was wondering if there is a better or more correct way to do it.
var objects =
from objString in recsString.Split(';')
let obj = objString.Split(':')
select new {
Code = obj[0].Trim(),
Description = obj[1].Trim()
};
That's perfectly fine, the only observation I would make though is that you remove empty entries by using the StringSplitOptions:
var objects =
from objString in recsString.Split(';', StringSplitOptions.RemoveEmptyEntries)
let obj = objString.Split(':', StringSplitOptions.RemoveEmptyEntries)
select new {
Code = obj[0].Trim(),
Description = obj[1].Trim()
};
If you think that there could be missing information, you could also be extra safe and null check the results:
var objects =
from objString in recsString.Split(';', StringSplitOptions.RemoveEmptyEntries)
let obj = objString.Split(':', StringSplitOptions.RemoveEmptyEntries)
select new {
Code = obj.Any() ? obj[0].Trim() : string.Empty,
Description = obj.Count > 1 ? obj[1].Trim() : string.Empty
};
What you're doing is just fine, here is how you'd write it using lambdas:
string objString = "code:description;code2:description2;code3:description3";
Dictionary<string, string> results =
objString.Split(';')
.Select(x => x.Split(':'))
.ToDictionary(key => key[0], value => value[1]);
// And now you have a nice little dictionary
foreach (var r in results)
Console.WriteLine("{0}:{1}",r.Key, r.Value);
Or of course:
var results = objString.Split(';')
.Select(x => x.Split(':'))
.Select(x => new {Code = x[0], Description = x[1]});
foreach (var r in results)
Console.WriteLine("{0}:{1}",r.Code, r.Description);
Related
From a given list of strings I need to use LINQ to generate a new sequence of strings, where each string consists of the first and last characters of the corresponding string in the original list.
Example:
stringList: new[] { "ehgrtthrehrehrehre", "fjjgoerugrjgrehg", "jgnjirgbrnigeheruwqqeughweirjewew" },
expected: new[] { "ee", "fg", "jw" });
list2 = stringList.Select(e => {e = "" + e[0] + e[e.Length - 1]; return e; }).ToList();
This is what I've tried, it works, but I need to use LINQ to solve the problem and I'm not sure how to adapt my solution.
just for the sake of completeness here is a version using Zip
var stringList = new string [] { "ehgrtthrehrehrehre", "fjjgoerugrjgrehg", "jgnjirgbrnigeheruwqqeughweirjewew" };
var result = stringList.Zip(stringList, (first, last) => $"{first.First()}{last.Last()}");
As mentioned in the comment that Select is already part of LINQ, you can use this code.var output = arr.Select(x => new string(new char[] { x.First(), x.Last() })).ToList();
Here you go:
var newList = stringList.Select(e => $"{e[0]}{e[e.Length - 1]}").ToList();
Approach with LINQ and String.Remove():
string[] input = new[] { "ehgrtthrehrehrehre", "fjjgoerugrjgrehg", "jgnjirgbrnigeheruwqqeughweirjewew" };
string[] result = input.Select(x => x.Remove(1, x.Length - 2)).ToArray();
I have a list of objects I want to reorder. The order is determined in a string.
(I hope my current code explains better than the text)
List<DataObject> objects = GetDataObjectsFromDatabase();
string order = "1,3,2";
List<DataObject> orderedObjects = new List<DataObject>
foreach(string index in order.Split(',')
{
orderedObjects.Add(objects.FirstOrDefault(x => x.id == index));
}
Is there a single Linq statement or a better way to do this?
Here is example of ordering list based on another list in linq:
List<OBJ> Test = new List<OBJ>();
Test.Add(new OBJ(1));
Test.Add(new OBJ(2));
string Order = "1,2,3";
List<string> OrderBy = Order.Split(new char[] { ',' }).ToList();
Test = Test.OrderBy(x => OrderBy.IndexOf(x.Field1.ToString())).ToList();
Console.ReadKey();
Assuming that x.Field1 is your ID (or whatever field you want to sort by).
Provided that .Select() will happen in order:
var orderedObjects = order.Split(',').Select(int.Parse).Select(index => objects.FirstOrDefault(obj => obj.id == index).ToList();
var data = order.Split(',').ToList();
I have a string[] containing ie:
abcd
Saving C:\\xx
aacd
Saving C:\\yy
aecd
Saving C:\\zz
and so on
Is there a way in linq that searches all the lines and returns only C:\\xx, C:\\yy and C:\\zz to a list/array.
This is what i tried so far:
string[] line = result.Split(new[] { '\r', '\n' });
string searchTerms = "Saving ";
var results = (from comp in line.ToString()
where searchTerms.All(s => comp.Contains(s))
select comp).ToList();
You don't need All method, you can just use:
where comp.StartsWith(searchTerms)
And instead of splitting on \r and \n it's better to use:
string[] line = result.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
And you should also remove ToString from line.ToString()
var results = (from comp in line
where comp.StartsWith(searchTerms)
select comp).ToList();
I don't, really understand your kind of input data, but if it is like this
string[] str = new string[] { "abcd", "Saving C:\\xx", "aacd", "Saving C:\\yy", "aecd", "Saving C:\\zz"};
then you can do the following:
List<string> result = str.Where(s => s.StartsWith("Saving C:\\")).ToList();
If string ends path:
string result = #"abcd
Saving C:\xx
aacd
Saving C:\yy
aecd
Saving C:\zz
and so on";
string[] line = result.Split(new[] { '\r', '\n' });
string searchTerms = "Saving ";
var lst = line.Where(x => x.StartsWith(searchTerms))
.Select(y => y.Substring(searchTerms.Length));
foreach (var x in lst)
Console.WriteLine(x);
http://ideone.com/OXDMO5
This should give you a simple IEnumerable containing each path.
NOTE: This will only return the part of each line after whatever is in searchTerms.
var result =
line.Where(l => l.StartsWith(searchTerms))
.Select(c => new string(c.Skip(searchTerms.Count()).ToArray()));
If all you want to do is get the paths from a string you can use regular expressions for this:
var result = "abcd\r\nSaving C:\\xx\r\naacd\r\nSaving C:\\yy\r\naecd\r\nSaving C:\\zz\r\n";
var regex = new Regex(#"(?<=Saving )(.*)");
var matches = regex.Matches(result).Cast<Match>().Select(m => m).ToList();
Edit
Another more generic form of regular expression you can use to get all paths (not just the ones with Savingbefore them):
var matches = Regex.Matches(result, ".:\\\\(.*?)(?=\s.*)", RegexOptions.Multiline).Cast<Match>().Select(m => m).ToList();
This also removes any whitespace after the paths.
Edit 2
To also get paths within quotes (paths with spaces) you can use this regular expression:
var regex = new Regex("((\"\\w|\\w):\\\\)((.*\")|(.*?)(?=\\s.*))");
I'm constructing a linq query that will check is a string in the DB contains any of the strings in a list of strings.
Something like.
query = query.Where(x => x.tags
.Contains(--any of the items in my list of strings--));
I'd also like to know how many of the items in the list were matched.
Any help would be appreciated.
Update: I should have mentioned that tags is a string not a list. And I am adding on a couple more wheres that are not related to tags before the query actually runs. This is running against entity framework.
EDIT: This answer assumed that tags was a collection of strings...
It sounds like you might want:
var list = new List<string> { ... };
var query = query.Where(x => x.tags.Any(tag => list.Contains(tag));
Or:
var list = new List<string> { ... };
var query = query.Where(x => x.tags.Intersect(list).Any());
(If this is using LINQ to SQL or EF, you may find one works but the other doesn't. In just LINQ to Objects, both should work.)
To get the count, you'd need something like:
var result = query.Select(x => new { x, count = x.tags.Count(tag => list.Contains(tag)) })
.Where(pair => pair.count != 0);
Then each element of result is a pair of x (the item) and count (the number of matching tags).
I've done something like this before:
var myList = new List<string>();
myList.Add("One");
myList.Add("Two");
var matches = query.Where(x => myList.Any(y => x.tags.Contains(y)));
like this:
List<string> list = new List<string>();
list.Add("One");
list.Add("Two");
var result = query.Where(x => list.Contains(x.tags));
I am not quite sure from your question if x.tags is a string or list, if it is a list Jon Skeet's answer is correct. If I understand you correctly though x.tags is a string of strings. If so then the solution is:
list.Any(x => x.tags.IndexOf(x) > -1)
to count them do
list.Count(x => x.tags.IndexOf(x) > -1)
var t = new List<string> { "a", "b", "c" };
var y = "a b d";
var res = y.Count(x => t.Contains(x.ToString()));
I faced a similar problem recently and here's how I managed to work it out:
var list = [list of strings];
if (list != null && list.Any())
{
queryable = queryable.Where(x => x.tags != null);
var tagQueries = new List<IQueryable<WhateverTheDbModelIs>>();
foreach (var element in list)
{
tagQueries.Add(queryable.Where(x => x.tags.Contains(element)));
}
IQueryable<WhateverTheDbModelIs> query = tagQueries.FirstOrDefault();
foreach (var tagQuery in tagQueries)
{
query = query.Union(tagQuery);
}
queryable = queryable.Intersect(query);
}
probably not the best option but something a less experienced developer can understand and use
I have a array of strings . I need to check in the array if it has something like "abcd". How to achive this in C#. I tried using the
var pathBits = new[] {"abcde ","abcd &"};
var item ="abcd";
var results = Array.FindAll(pathBits, s => s.Equals(item ));
maybe something like this:
var result = pathBits.Any(y => y.Contains(item));
That will give you true if the array contains an item that has a value like item. If you want to select all those values you should use:
var result = pathBits.Where(y => y.Contains(item));
which will give you an IEnumerable of the items from the list that contain the value item.
When you say 'something like "abcd"' do you mean "Starts with" or "Contains"?
The current code will only find strings in pathBits which are exactly equal to item ("abcd" ?)
The general shape is fine but to find non-exact matches you need to change the predicate
e.g.
string[] src = new[] { "abcde", "abcd &" };
var results = Array.FindAll<string>(src, name => name.Contains("abcd"));
This can also be implemented using the Linq IEnumerable<> extensions
e.g.
string[] src = new[] { "abcde", "abcd &" };
var results = src.Where(name => name.Contains("abcd"));
hth,
Alan.
This might be of some use
string[] pathBits = { "abcde ", "abcd &" };
var item = "abcd";
if (pathBits.Contains(item)) ;
{
}
You cannot use
var pathbits = { "abcde ", "abcd &" };
Please let me know if you have any problem
Is this what your looking for?
string[] pathBits = { "abcde ", "abcd &", "222" };
var item = "abcd";
var results = Array.FindAll<string>(pathBits, s => s.Contains(item));
results will have 2 items.
I'm not sure exactly what you want, but this would get all array allements that contain the string "abcd" -
String[] pathBits = {"abcde ","abcd &"};
var item ="abcd";
var results = pathBits.Where(s => s.IndexOf("abcd") > -1);