1

I have a SQL query that I am trying to convert into a LINQ query expression in C#, but I can not do it properly.

The query is as follows:

select 
    P.id, P.name, COUNT(S.orderqty)
from 
    product as P 
inner join 
    sales as S on P.id = S.id
group by 
    p.id, p.name

My linq code:

IQueryable<ReportType1> query = 
    from product in EngineContext.Products
    join SalesOrder inEngineContext.SalesOrderDetails  
    on product.ProductID equals SalesOrder.ProductID
    group SalesOrder.OrderQty by product into groups
    select new ReportType1() {    
        Year = null,
        ProducId = groups.Key.ProductID,
        Name = groups.Key.Name,
        Quantity = ??
    };

What to write for value of Quantity?

1 Answer 1

1

Count method of group:

select new ReportType1
{
    Year = null,
    ProducId = groups.Key.ProductID,
    Name = groups.Key.Name,
    Quantity = groups.Count()
}
Sign up to request clarification or add additional context in comments.

5 Comments

@MohammadRezaTaghipour what do you mean? It works because it's LinqToSql and it translate methods to sql-code
yes,my question is not clear! i mean what does "groups.Count()" refer to? does it refer to exactly "SalesOrder.OrderQty"?
@MohammadRezaTaghipour it refers to your groupping by product, so it counts in every group
@MohammadRezaTaghipour Please consider checking the answer as the right answer if it was helpful to encourage more pepole to answer your questions
@MohammadRezaTaghipour can you accept my answer or should I improve it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.