views:

92

answers:

2

I have a small winapp that uses LinqToSQL as it's DAL. I am creating a summary view of all the CaseNotes for a given person and one of the fields is a Details box. I need to return only the first 50 characters of that column to my treeview function.

Any hints on how I do that? The below is how my TreeView function gets its data for display and the ContactDetails is the column in question.

        public static DataTable GetTreeViewCNotes(int personID)
    {
        var context = new MATRIXDataContext();
        var caseNotesTree = from cn in context.tblCaseNotes
                        where cn.PersonID == personID
                        orderby cn.ContactDate
                        select new { cn.CaseNoteID,cn.ContactDate, cn.ParentNote, cn.IsCaseLog, cn.ContactDetails };

        var dataTable = caseNotesTree.CopyLinqToDataTable();
        context.Dispose();
        return dataTable;
    }


ANSWER

I am posting this here in case any future searchers wonder what the solution looks like in the questions context.

        public static DataTable GetTreeViewCNotes(int personID)
    {
        DataTable dataTable;
        using (var context = new MATRIXDataContext())
        {
            var caseNotesTree = from cn in context.tblCaseNotes
                                where cn.PersonID == personID
                                orderby cn.ContactDate
                                select new
                                           {
                                               cn.CaseNoteID,
                                               cn.ContactDate, 
                                               cn.ParentNote, 
                                               cn.IsCaseLog, 
                                               ContactDetailsPreview = cn.ContactDetails.Substring(0,50)
                                           };

            dataTable = caseNotesTree.CopyLinqToDataTable();
        }
        return dataTable;
    }
+4  A: 

String.Substring:

var caseNotesTree = from cn in context.tblCaseNotes
                    where cn.PersonID == personID
                    orderby cn.ContactDate
                    select new {
                        cn.CaseNoteID,
                        cn.ContactDate,
                        cn.ParentNote,
                        cn.IsCaseLog,
                        ContactDetailsClip = cn.ContactDetails.Substring(0, Math.Min(cn.ContactDetails.Length, 50))
                    };

Also, I would suggest wrapping your use of DataContexts in using blocks.

Jason
The problem with this is if the text in ContactDetails is not 50 characters in length you will get an exception. What you do is something like... ContactDetailsClip = cn.ContactDetails.Length > 50 ? cn.ContactDetails.Substring(0, 49) : cn.ContactDetails;
Chalkey
@Chalkey: When I first read your comment I was like "Of course" but interestingly it does not error out.....I'd love to know why.
Refracted Paladin
@Chalkey: Use Math.Min(length of string, clip length). See edit.
Jason
@Jason: isn't it Math.Min instead ?
Thomas Levesque
@Refracted Paladin: That code will compile, it wont crash until run time. Its just like this line- String s = "Hello".Substring(0, 100); - will compile, but crash at runtime.
Chalkey
@Jason: cool little tip :)
Chalkey
@Chalkey: I understand what you are saying. What I am saying is that it is not crashing at runtime. Even with empty or less then 10 characters present in the details field. In fact it works perfect and I am curious why as what you say makes sense and I would have expected a fail.
Refracted Paladin
@Refracted Paladin: Not sure, I've just made it throw an ArgumentOutOfRangeException by substringing to much than is available.
Chalkey
A: 

cn.ContactDetails.Substring(0, 50);

In the "select new" line. Does that work?

mgroves
You have to give the result a name, too.
Jason