0

Hi I am getting the following error with my Linq query.

Cannot implicitly convert type 'System.Collections.Generic.List<string>'
to 'System.Collections.Generic.List<CTS.Domain.OCASPhoneCalls>'

I know what it means, but I'm unsure how to fix it. Can someone help me with my query? I'm really new to linq.

public List<OCASPhoneCalls> getPhoneLogs2()
{
    using (var repo = new OCASPhoneCallsRepository(new UnitOfWorkCTS()))
    {
        List<OCASPhoneCalls> phone = repo.AllIncluding(p => p.OCASStaff)
            .Where(y => y.intNIOSHClaimID == null)
            .Select(w => w.vcharDiscussion.Substring(0, 100) + "...")
            .ToList();                  
        return phone;
    }
}
1
  • You are selecting a List<string> but you are declaring a List<OCASPhoneCalls> Commented Mar 7, 2014 at 15:32

3 Answers 3

6

You are selecting a single property with

.Select(w => w.vcharDiscussion.Substring(0, 100) + "...")

This would return you IEnumerable<string> and calling ToList would return you List<string> NOT List<OCASPhoneCalls>.

If you are returning formatted strings then your method return type should be List<string> like:

public List<string> getPhoneLogs2()
{
    using (var repo = new OCASPhoneCallsRepository(new UnitOfWorkCTS()))
    {
        List<string> phone = repo.AllIncluding(p => p.OCASStaff)
            .Where(y => y.intNIOSHClaimID == null)
            .Select(w => w.vcharDiscussion.Substring(0, 100) + "...")
            .ToList();                  
        return phone;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I'm fairly sure that he wants to return the objects but shorten the text property if necessary.
@TimSchmelter, hmm in that case OP has to iterate first and then modify the field like in your answer. +1
@TimSchmelter I'm using this data to populate a GridView, it's now saying that other columns in the GridView do not exist. The error message is DataBinding: 'System.String' does not contain a property with the name 'dtmDateCalled'. Do I have to select those in my query? Can you tell me how to fix that? Thank you.
@Habib your way is working for me, except that my GridView can't find my other columns. Do I have to select all columns in the linq query? Can you tell me how to do that?
@hollyquinn, I guess your GridView is expecting a List<OCASPhoneCalls>, If that is the case, then use Tim's Answer. Do not change your method return type, Just use the query he has in his answer.
3

You are selecting a List<string> but you are declaring a List<OCASPhoneCalls>, i assume you want to shorten the vcharDiscussion:

List<OCASPhoneCalls> phones = = repo.AllIncluding(p => p.OCASStaff)
    .Where(p =>  p.intNIOSHClaimID == null)
    .ToList();
phones.ForEach(p => p.vcharDiscussion = p.vcharDiscussion.Length > 100 ?
         p.vcharDiscussion.Substring(0, 100) + "..." :
         p.vcharDiscussion);
return phones;

Edit: "I'm getting a null error. vcharDiscussion is coming up null"

Then you need to check that:

phones.ForEach(p => p.vcharDiscussion = 
    p.vcharDiscussion != null && p.vcharDiscussion.Length > 100 ?
    p.vcharDiscussion.Substring(0, 100) + "..." :
    p.vcharDiscussion ?? "");

8 Comments

I do want to shorten vcharDiscussion and add ... to the end of it.
@hollyquinn: then this should work. You need to iterate them to modify the property.
I'm getting the following error message on return phones on this. Cannot implicitly convert type 'System.Collections.Generic.List<CTS.Domain.OCASPhoneCalls>' to 'System.Collections.Generic.List<string>'
@hollyquinn: why have you changed the return type of the method, i thought that you want to return a List<CTS.Domain.OCASPhoneCalls>? I guess you have tested Habib's approach ;-)
@Time Schmelter you have return phones?? I didn't change it.
|
0
`.Select(w => w.vcharDiscussion.Substring(0, 100) + "...")` 

because select it's projection and it will return a list of string and your method expect to return

List<OCASPhoneCalls>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.