MiLaD A
MiLaD A

Reputation: 145

Entity Framework cast selected fields to varchar and concat them

I want do below query in 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

Upvotes: 1

Views: 497

Answers (1)

Sunil
Sunil

Reputation: 3424

Since you have not mentioned a language, I am writing code in C#.

Try this:

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() can be changed according to your expectations

Upvotes: 1

Related Questions