简体   繁体   中英

Linq query for complex result set

Can anyone help me with linq query for the below result set.

First is DInfo

ID  DId DName
1   D1  DName1
2   D2  DName2
3   D3  Dname3

Second is MInfo

ID  DId MId MName
1   D1  1   MName1
2   D1  2   MName2
3   D2  1   MName3
4   D2  2   MName4
5   D2  3   MName5

Third is VData (Transaction Table)

ID  Did Mid Value   DateTime
1   D1  1   10.25   2018-04-15 17:33:22
2   D1  1   11.26   2018-04-15 19:33:22
3   D1  1   12.30   2018-04-15 22:33:22
4   D1  2   45.50   2018-04-15 17:33:22
5   D1  2   50.40   2018-04-15 19:33:22
6   D1  2   60.66   2018-04-15 22:33:22
6   D2  1   60.41   2018-04-15 19:33:22
7   D2  1   66.22   2018-04-15 22:33:22
8   D2  1   70.65   2018-04-15 23:33:22

My Idea of resultset is get all the distinct rows from DInfo and find all the rows from MInfo matching to DID of DInfo and than for each unique row get first & last value from transaction table (based on datetime)

My expected ResultSet is as below

MName   OpeningValue    ClosingValue
MName1  10.25            12.30
MName2  45.50            60.66
MName3  60.41            70.65

The above data is just for reference purpose in fact in production scenario there can be varying no. of rows in all the tables.

Thanks

Try Following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication108
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable mInfo = new DataTable();
            mInfo.Columns.Add("ID", typeof(int));
            mInfo.Columns.Add("Did", typeof(string));
            mInfo.Columns.Add("Mid", typeof(int));
            mInfo.Columns.Add("MName", typeof(string));

            mInfo.Rows.Add(new object[] {1, "D1", 1, "MName1"});
            mInfo.Rows.Add(new object[] {2, "D1", 2, "MName2"});
            mInfo.Rows.Add(new object[] {3, "D2", 1, "MName3"});
            mInfo.Rows.Add(new object[] {4, "D2", 2, "MName4"});
            mInfo.Rows.Add(new object[] {5, "D2", 3, "MName5"});

            DataTable vData = new DataTable();
            vData.Columns.Add("ID", typeof(int));
            vData.Columns.Add("Did", typeof(string));
            vData.Columns.Add("Mid", typeof(int));
            vData.Columns.Add("Value", typeof(decimal));
            vData.Columns.Add("DateTime", typeof(DateTime));

            vData.Rows.Add(new object[] { 1, "D1", 1, 10.25, DateTime.Parse("2018-04-15 17:33:22")});
            vData.Rows.Add(new object[] { 2, "D1", 2, 11.26, DateTime.Parse("2018-04-15 19:33:22")});
            vData.Rows.Add(new object[] { 3, "D2", 1, 12.30, DateTime.Parse("2018-04-15 22:33:22")});
            vData.Rows.Add(new object[] { 4, "D2", 2, 45.50, DateTime.Parse("2018-04-15 17:33:22")});
            vData.Rows.Add(new object[] { 5, "D2", 3, 50.40, DateTime.Parse("2018-04-15 19:33:22")});
            vData.Rows.Add(new object[] { 6, "D1", 2, 60.66, DateTime.Parse("2018-04-15 22:33:22")});
            vData.Rows.Add(new object[] { 6, "D2", 1, 60.41, DateTime.Parse("2018-04-15 19:33:22")});
            vData.Rows.Add(new object[] { 7, "D2", 1, 66.22, DateTime.Parse("2018-04-15 22:33:22")});
            vData.Rows.Add(new object[] { 8, "D2", 1, 70.65, DateTime.Parse("2018-04-15 23:33:22")});

            var results = vData.AsEnumerable().OrderBy(x => x.Field<DateTime>("DateTime"))
                .GroupBy(x => new { Did = x.Field<string>("Did"), Mid = x.Field<int>("Mid") })
                .Select(x => new
                {
                    MName = mInfo.AsEnumerable().Where(y => (y.Field<string>("Did") == x.Key.Did) && (y.Field<int>("Mid") == x.Key.Mid)).FirstOrDefault().Field<string>("MName"),
                    OpeningValue = x.First().Field<decimal>("Value"),
                    ClosingValue = x.Last().Field<decimal>("Value")
                }).ToList();

        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM