实体框架将选定的字段转换为varchar并将它们连接起来

时间:2017-12-06 06:01:07

标签: sql entity-framework

我想在Entity Framework中进行以下查询

select 
    cast(p_min as varchar) + '' + cast(p_max as varchar)  
from 
    user_behave_fact
where 
    beef_dairy_stat = 'True' and param_id = 2
group by 
    p_min,p_max
go

1 个答案:

答案 0 :(得分:1)

由于您没有提到语言,我正在用C#编写代码。

试试这个:

using (var dbContext = new DatabaseContext())
{
   var output = (
                 from fact in dbContext.user_behave_facts
                 where fact.beef_dairy_stat == "True" && fact.param_id == 2
                 group fact by new {fact.p_min, fact.p_max} in grp
                 select new
                 {
                    ColName = grp.Key.p_min.ToString() + " " + grp.Key.p_max.ToString()
                 }
                 ).ToList();
}

.ToList()可以根据您的期望进行更改