Linq将字符串转换为列表

hol*_*inn 0 c# linq

嗨,我的Linq查询收到以下错误.

Cannot implicitly convert type 'System.Collections.Generic.List<string>'
to 'System.Collections.Generic.List<CTS.Domain.OCASPhoneCalls>'
Run Code Online (Sandbox Code Playgroud)

我知道这意味着什么,但我不确定如何解决它.有人可以帮我查询吗?我真的很喜欢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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Hab*_*bib 6

您正在选择一个属性

.Select(w => w.vcharDiscussion.Substring(0, 100) + "...")
Run Code Online (Sandbox Code Playgroud)

这将返回您IEnumerable<string>和通话ToList将返回你List<string> 不是 List<OCASPhoneCalls>.

如果要返回格式化字符串,则方法返回类型应为List<string>:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)