Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return first 50 characters of text in LINQ call

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;
    }
like image 682
Refracted Paladin Avatar asked Jul 09 '09 13:07

Refracted Paladin


People also ask

How do I retrieve the first 5 characters from a string?

string str = yourStringVariable. Substring(0,5);

How can I get first 3 characters of a string in asp net?

To get the first n characters of a string, we can use the built-in Substring() method by passing the 0, n as arguments to it. Where 0 is the start index, n is the number of characters we need to get from the string. In the example above, we have passed 0, 3 as arguments to the Substring() method.

How do you get the first row in LINQ?

Solution 1. int id = 2; var item = lstData. FirstOrDefault(k => k.ID == id); if (item != null) { // use the Item object to read the properties. // your code here... }

How do I get the last 3 characters of a string?

string str = "AM0122200204"; string substr = str. Substring(str. Length - 3);


2 Answers

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.

like image 52
jason Avatar answered Oct 27 '22 14:10

jason


With LinQ you can also do the following :

new string( myString.Take(50).ToArray() );
like image 43
KroaX Avatar answered Oct 27 '22 15:10

KroaX