-
Oct 20th, 2014, 08:39 PM
#1
[RESOLVED] How to do this in Linq?
Ok... if I have a string such as:
"c:\documents\medical\Mr Smith\123.doc"
is it possible to loop through this in linq such that the iterations will be:
Code:
1 c:\documents\medical\Mr Smith\123.doc
2 c:\documents\medical\Mr Smith
3 c:\documents\medical
4 c:\documents
5 c:
... also yes it needs to be linq as I want to use this in linq to sql
Thanks in advance,
Kris
-
Oct 20th, 2014, 09:11 PM
#2
Re: How to do this in Linq?
Keep in mind that, while all LINQ providers support all LINQ syntax, they don't necessarily support all LINQ operations. It depends on the provider implementation. Anything you do in LINQ to SQL has to be able to be converted to T-SQL code to be executed against your SQL Server database. Even if the code compiles, it will fail at run time if it contains something that can't be mapped to T-SQL.
With that in mind, what is it that you actually want to do in LINQ to SQL? Is this text in the database or are you passing it in to the database to filter a query or the like? If it's the former then it's probably not possible but if it's the latter then LINQ is actually irrelevant.
-
Oct 20th, 2014, 09:40 PM
#3
Re: How to do this in Linq?
Yes it's the former - the path is data from a field in the db.. ... would have thought there would be a way ... oh well ... will just have to play with it...
Kris
-
Oct 20th, 2014, 09:44 PM
#4
Re: How to do this in Linq?
try this:
Code:
Dim path As String = "c:\documents\medical\Mr Smith\123.doc"
Dim parts = Enumerable.Range(0, path.Count(Function(c) c = "\"c) + 1).Select(Function(x) _
String.Join("\", path.Split("\"c).Take(path.Count(Function(c) c = "\"c) + 1 - x).ToArray)).ToArray
after reading what jm posted, I'm not sure how useful that will be now...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 20th, 2014, 09:45 PM
#5
Re: How to do this in Linq?
So what is it that you actually want to do? I said probably not possible but that doesn't mean definitely.
-
Oct 20th, 2014, 10:39 PM
#6
Re: How to do this in Linq?
Well ... this is simplified ... but basically I have a table with locations such as
IsLocation | Location
FALSE | "Australia\SiteX\Wing1\Room3"
TRUE | "Australia\SiteX"
TRUE | "Australia\SiteX\MaintenanceShead"
....
... I then have another table ... let's say it has this in it:
Location
"Australia\SiteX\Wing1\Room3"
"Australia\SiteX\Wing2\Room1"
Now I need to loop through all of the results in the 2nd table and return the locations for the items that are marked as IsLocation ... or a sub of it (eg Australia\SiteX\Wing1\Room3 will be in "Australia\SiteX" location)
Now I could uses starts with to do it ... but the issue is I want the most specific location ... eg there can be locations within other locations ... and I want the first item ONLY as I work out for eg: in the case of "Australia\SiteX\MaintenanceShead\A1" I only want it to return "Australia\SiteX\MaintenanceShead" as that is the most specific location with SiteX being disregarded in this case.
... the data is a little more strict than this ... but this is a simple example that will easily explain what I need
EDIT Actually think I just figured it out, feel like an idiot* ... I could use starts with and then just get the one with the greatest length... lol ... will try and post back my results!
Kris
Last edited by i00; Oct 20th, 2014 at 10:45 PM.
-
Oct 20th, 2014, 10:47 PM
#7
Re: How to do this in Linq?
Even using StartsWith, you might end up with issues like "X\Y\Room10" starting with "X\Y\Room1". If you want to do that in L2S then the only way I can see it happening is if you write a function in SQL Server and map that to your data context. I'm not 100% sure that that's even supported in L2S. I'm fairly sure it is in EF but I've never actually done it there either.
-
Oct 20th, 2014, 10:52 PM
#8
Re: How to do this in Linq?
Yep works fine ... except starts with doesn't seem to be able to be converted to l2sql so I used IndexOf(x)=0 instead
Kris
-
Oct 20th, 2014, 10:53 PM
#9
Re: How to do this in Linq?
Originally Posted by jmcilhinney
Even using StartsWith, you might end up with issues like "X\Y\Room10" starting with "X\Y\Room1". If you want to do that in L2S then the only way I can see it happening is if you write a function in SQL Server and map that to your data context. I'm not 100% sure that that's even supported in L2S. I'm fairly sure it is in EF but I've never actually done it there either.
Won't be an issue ... used:
vb Code:
PersonLocations.Any(Function(y) y.Name = x.Name OrElse y.Name.IndexOf(x.Name & "\") = 0)
Kris
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|