Hi,
I have the below datatable created through my C# code:

Stock TotalPrice Qty TransactionType
XYZ 400 10 BUY
ABC 800 20 BUY
XYZ 800 20 SELL
ABC 400 10 SELL

I want this datatable to be processed by LINQ / LAMBDA expressions in such a way that will give me the below output in another datatable.
BASICALLY, THE NEED IS TO SHOW THE NET POSITIONS ON THE TRANSACTIONS MADE ON THE STOCK.

Stock Avg Price Qty TransactionType
XYZ 40 10 SELL
ABC 40 10 BUY

Can anybody please help me in writing a LINQ query \ LAMBDA expression for the above case.

Thanks in advance,
Sumit

The transformation you have given isn't well-explained. What would be the output if the input were the following:

ABC 1000 10 BUY
ABC  500 10 SELL

Thanks for replying.

The output will have no rows as 10 was bought and 10 was sold. So, net position is zero (profit was made but we do require it to show).

Is the requirement clear ? Can u pls send me a solution for this.

Okay. And what should the output be with the following input?

ABC 1000 11 BUY
ABC  500 10 SELL

The resule will be 1 share bought at 1000

Really? Okay... Then what about

ABC 1000 10 BUY
ABC 1000 10 SELL
ABC 1100 10 BUY

I can't see what algorithm you're using to conglomerate these things.

Lets say for simplicity, we remove the price column and do not require it. Can you please provide the LINQ query \ LAMBDA expression for this updated requirement.

Yes, I can. Why can't you?

Unfortunately, i am tqtally new to LINQ /Lambda. Can you please help me write a expression for it. I will really be thankful to you. Thx :)

Hi,
Check this query

       var result = from row in dt.AsEnumerable() select new 
        { stk = row.Field<string>("Stock"), Avg = (row.Field<int>("TotPrice")) / (row.Field<int>("Qty")), Qty = row.Field<int>("Qty"), TranType = row.Field<string>("Type") };


        result variable will have the data as of your requirement after executing above statement.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.