Link to home
Create AccountLog in
Avatar of richy102
richy102

asked on

How do i use the SubString() in linq to sql c#

i want to retrieve a range of characters from my databese using the SubString()
 
public Blog GetBlogByBlogID(Int32 BlogID)
        {
            Blog result = new Blog();
            using (RateMeDataContext rtm = _conn.GetContext())
            {
                result = rtm.Blogs.Where(b => b.BlogID == BlogID).FirstOrDefault();
                var post = (from b in rtm.Blogs where b.BlogID == BlogID select b.Post.Substring(0,10))+ "......";
                aspnet_User account = rtm.aspnet_Users.Where(a => a.UserName == result.UserName).FirstOrDefault();
                result.UserName = Helper.CurrentUserName;
            }
            return result;
        }

Open in new window

but it dose not work. please review my code. please tell me if i did anything wrong. it retrieves all the data from the post column not the range of characters i specified.
thanks
Avatar of shubham_gniiit
shubham_gniiit

Avatar of richy102

ASKER

thanks, but that didn't solve my problem i thought it could be just a single line of code
just as u can do it in a stored-procedure  and in ado.net  in retrieving such data.
i just want to get only the first 30 characters from the feild and display it with a
link that will display the main page for the full text. for now everything works but it dose not
display the range of characters i specified.
Avatar of Jesus Rodriguez
Why don't you do this with the left function

public Blog GetBlogByBlogID(Int32 BlogID)
        {
            Blog result = new Blog();
            using (RateMeDataContext rtm = _conn.GetContext())
            {
                result = rtm.Blogs.Where(b => b.BlogID == BlogID).FirstOrDefault();
                var post = (from b in rtm.Blogs where b.BlogID == BlogID select left(b.Post,10))+ "......";
                aspnet_User account = rtm.aspnet_Users.Where(a => a.UserName == result.UserName).FirstOrDefault();
                result.UserName = Helper.CurrentUserName;
            }
            return result;
        }
@k-designers:
thanks. i assume the left method dose not exist in c# only in Vb but i Created
mine but it still didnt work. i decided to use the subString() with some logic
directly into the control that binds to the data and it worked.
ASKER CERTIFIED SOLUTION
Avatar of Kumaraswamy R
Kumaraswamy R
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.