您好我的Linq查询出现以下错误。
Cannot implicitly convert type 'System.Collections.Generic.List<string>'
to 'System.Collections.Generic.List<CTS.Domain.OCASPhoneCalls>'
我知道这意味着什么,但我不确定如何解决它。有人可以帮我查询吗?我真的很喜欢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;
}
}
答案 0 :(得分:6)
您正在使用
选择一个属性.Select(w => w.vcharDiscussion.Substring(0, 100) + "...")
这会返回给您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;
}
}
答案 1 :(得分:3)
您选择的是List<string>
,但是您宣布List<OCASPhoneCalls>
,我认为您想要缩短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;
编辑:“我收到一个空错误.vcharDiscussion即将出现”
然后你需要检查:
phones.ForEach(p => p.vcharDiscussion =
p.vcharDiscussion != null && p.vcharDiscussion.Length > 100 ?
p.vcharDiscussion.Substring(0, 100) + "..." :
p.vcharDiscussion ?? "");
答案 2 :(得分:0)
`.Select(w => w.vcharDiscussion.Substring(0, 100) + "...")`
因为选择了它的投影,它将返回一个字符串列表,你的方法希望返回
List<OCASPhoneCalls>