Results 1 to 9 of 9

Thread: [RESOLVED] How to do this in Linq?

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    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.

  3. #3

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    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

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,467

    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...

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    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.

  6. #6

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    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.

  8. #8

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    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

  9. #9

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: How to do this in Linq?

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. 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
  •  



Click Here to Expand Forum to Full Width