How to sort on Xml field.

Posts   
 
    
Subodh
User
Posts: 9
Joined: 20-Nov-2007
# Posted on: 08-Dec-2008 14:46:13   

In my database(SQl server 2005) I have XML field anmed documentName, while fetching collection I want to sort using documentName, how do I do that.

documentName xml(DOCUMENT dbo.cb3)

This field holds document name in multilingual format. The same data is

<localizedstring xmlns="http://www.orangehill.nl/schema/cb3/localizedstring"><value locale="En-Us">Name in English</value><value locale="Fr-fr">Name in French</value></localizedstring>

I have assigned class(type) LocalizedString to this field as [Serializable] [CLSCompliant(true)] public class LocalizedString : IEnumerable<KeyValuePair<string, string>>, IEnumerable { public LocalizedString(); public LocalizedString(string xmlString);

    public int Count { get; }
    public string FieldName { get; set; }
    public bool IsDirty { get; set; }
    public string OuterXml { get; set; }

    public string this[string locale] { get; set; }

    public void Clear();
    public override string ToString();
}

Please let me know how do I sort on XML fields like this.

I am using ver. 2.5

Also let me know if this can be easily done in version 2.6 with 3.5 and Linq

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 08-Dec-2008 15:53:28   

Do you want to sort on the daatabase (when fetching), or sort the collection in memory?

If in the database, do you have the SQL query which can do this job for you?

Subodh
User
Posts: 9
Joined: 20-Nov-2007
# Posted on: 09-Dec-2008 05:51:36   

At the end I want the sorted data (in database or in memory). I dont know SQL qery to do this. I think this is very difficult(not possible!) in SQL to do this.

So tell me the fastest approach to do this.

Let me know if this can be acheived using LINQ and LLBLGen 2.6 with framework 3.5.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 09-Dec-2008 11:52:21   

I don't know how to do this with Linq.

But if you want to in-memory sort, you can implement your own IComparer. You can find a simple example here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=12401

Otherwise if you want to sort in the database side, I think you might need XQuery() which we don't support, but you may try using a DBFunctionCall.

Subodh
User
Posts: 9
Joined: 20-Nov-2007
# Posted on: 09-Dec-2008 16:41:03   

ok one of my friend suggested like bellow and it worked

naemField.ExpressionToApply = new DbFunctionCall("Name.query('//*[@locale=sql:variable(''{0}'')]').value('.','varchar(250)')", new object[] { culture });

and apply nameField to sortExpression.