views:

125

answers:

1

Hi, could you help me please convert this SQL query to Linq to Entity query? Thank you

select distinct item, count(item) as count 
from TableName 
where ColumnName = @parameter and (ColumnName2 = @parameter2 OR ColumnName3 = @parameter3)
group by item  order by item asc
A: 
from TableName in db.TableName
where TableName.Column1 == 'param1' 
    && ( TableName.Column2 == 'param2' || TableName.Column3 == 'param3' )
group item by new { TableName.Item } into t
orderby t.Key.Item
select new {  t.Key.Item,  ItemCount = t.Count() }
Thomas
Thank you, that works but only for one parameter, how can I accomplish using 2 parameters (ColumnName2 = @parameter2 OR ColumnName3 = @parameter3)
George
Thomas
ok one more question. if I need to select one of the parameter which doesn't have a null value. How do I need to change the query?
George
@George - Can you amend your question to illustrate what you are trying to achieve?
Thomas
Thank you. I found work around for my case. Your solution in general works for me.
George
@George - If that is the case, then it is customary to mark an answer and provide an up vote.
Thomas