合并两列时出错 [英] Getting error during merge of two columns

查看:147
本文介绍了合并两列时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要返回上次修改记录的人的姓名+姓氏。但我收到以下错误:



无法隐式转换类型'AnonymousType#1'为'string'

I need to return firstname + lastname of person who has modified record last time. But I am getting following error:

Cannot implicitly convert type 'AnonymousType#1' to 'string'

var scenarioList = (from sc in _context.Scenarios
                    join t in _context.Tools on sc.ToolId equals t.ToolId
                    join st in _context.ServicesTools on sc.ToolId equals st.ToolId
                    join s in _context.Services on st.ServiceId equals s.ServiceId
                    join cu in _context.contact on sc.CreatedById equals cu.ProfileId
                    where t.IsActive == true && s.IsActive == true && resultListActiveContractUser.Contains(s.Code)
                    select new ToolScenario
                    {
                        ScenarioId = sc.ScenarioId,
                        Scenario = sc.Scenario,
                        ToolId = sc.ToolId,
                        CreatedDate = sc.CreatedDate,
                        CreatedById = sc.CreatedById,
                        IsDeleted = sc.IsDeleted,
                        IsScenarioSaved = sc.IsScenarioSaved,
                        ModifiedDate = sc.ModifiedDate,
                        ModifiedById = sc.ModifiedById,
                        ModifiedBy = _context.contact.Where(x => x.ProfileId == sc.ModifiedById).Select(x => new { contact = x.FirstName + " " + x.LastName }).FirstOrDefault(),
                        Tool = t.Tool,
                        Url = t.URL,
                        Taglist = _context.ScenarioTags.Where(n => n.CreatedById == sc.CreatedById && n.ScenarioId == sc.ScenarioId).OrderBy(x => x.Tag).ToList(),
                        sharedType = _context.Scenario_Contacts.Where(x => x.ScenarioId == sc.ScenarioId).Count() > 0 ? "share1" : "share0"
                    }).ToList().Where(x => x.IsDeleted == false && x.CreatedById == profileId && x.IsScenarioSaved == true);

推荐答案

由于您使用匿名方法连接名字和姓氏,因此需要对字符串进行显式强制转换。 work:

Since you are using an anonymous method to concatenate first and last names, you need to do an explicit cast to string for it to work:
ModifiedBy = _context.contact.Where(x => x.ProfileId == sc.ModifiedById).Select(x => new { contact = x.FirstName + " " + x.LastName }).FirstOrDefault().ToString(),





Ano解决方案可以是向Contact类添加FullName属性:



Another solution could be to add a FullName property to your Contact class:

public string FullName {
   get { return string.Format("{0} {1}", FirstName, LastName); }
}



然后:


Then:

ModifiedBy = _context.contact.Where(x => x.ProfileId == sc.ModifiedById).FullName,


这篇关于合并两列时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆