Linq添加缺少的不同值

时间:2017-07-17 11:22:22

标签: c# mysql linq

我有一些linq查询执行以下操作:

...
CarsCategories = new ObservableCollection<CarsCategory>(y
                                        .GroupBy(x1 => new { a = x1.Field<string>("CategoryName") })
                                        .Select(z => new CarCategory
                                        {
                                            CategoryName = z.Key.a,
                                            CarGroups =  new ObservableCollection<CarGroup>(z
                                                .GroupBy(x1 => new { a = x1.Field<string>("GroupName") })
                                                .Select(z1 => new CarGroup
                                                    {
                                                        GroupName = z1.Key.a,
                                                        CarTests = new ObservableCollection<CarTest>(z1
                                                            .GroupBy(x2 => new { a = x2.Field<string>("TestName") })
                                                            .Select(z2 => new CarTest
                                                            {
                                                                TestName = z2.Key.a,
                                                                CarTestNumbers = new ObservableCollection<CarTestNumber>(z2
                                                                    .Select(z3 => new CarTestNumber
                                                                    {
                                                                       TestNumber = z3.Field<String>("TestNumber"),
                                                                       TestVisitCode = z3.Field<String>("TestVisitCode")
                                                                    }))

                                                            }))
                                                    }

                                        ))}))
...

基本上我有一个大表,按CarCategory分组,然后由CarGroup分组,然后由CarTest分组,每个CarTest有多个TestNumber和TestVisitCode参数。

一切都按预期工作,但我想做以下事情:

此表中的所有测试都有不同的TestVisitCode值。我希望在我的分组类别CarTestNumbers中的每个测试中,让表中存在所有不同的TestVisitCode,并且我有一个值来显示它,否则显示一个空字符串。

我的CarTestNumber模型如下所示:

public class CarTestNumber
    {
        public string TestNumber { get; set; }

        public string TestVisitCode { get; set; }

        public string VisitCodeAndTestNumber
        {
            get { return TestVisitCode +" - " +  TestNumber; }
        }

    }

目前我只展示现有的TestVisitCodeTestNumber

例如:

Row1: CategoryName - FirstCategory GroupName - FirstGroup TestName - "FirstTest" TestNumber - "12" TestVisitCode - "W1"
Row2: CategoryName - FirstCategory GroupName - FirstGroup TestName - "FirstTest" TestNumber - "13" TestVisitCode - "W2"
Row3: CategoryName - FirstCategory GroupName - FirstGroup TestName - "SecondTest" TestNumber - "14" TestVisitCode - "W1"
Row4: CategoryName - FirstCategory GroupName - FirstGroup TestName - "SecondTest" TestNumber - "15" TestVisitCode - "W2"
Row5: CategoryName - FirstCategory GroupName - FirstGroup TestName - "SecondTest" TestNumber - "16" TestVisitCode - "W3"

After executing linq the data will look like this

FirstCategory
    FirstGroup
        FirstTest - {(W1,12), (W2,13)}
        SecondTest - {(W1,14), (W2,15),(W3, 16)}

What I would like to have

FirstCategory
    FirstGroup
        FirstTest - {(W1,12), (W2,13),(W3, "")}
        SecondTest - {(W1,14), (W2,15),(W3, 16)}

1 个答案:

答案 0 :(得分:1)

您可以先选择所有TestVisitCodes。 在您的linq查询中,您可以与当前CarTestNumber以外的所有VisitCodes建立联盟。 这有点令人困惑。

我创建了一个DataTable并用你的testdata填充它:

DataTable loDt = new DataTable();
loDt.Columns.Add("CategoryName", typeof(string));
loDt.Columns.Add("GroupName", typeof(string));
loDt.Columns.Add("TestName", typeof(string));
loDt.Columns.Add("TestNumber", typeof(string));
loDt.Columns.Add("TestVisitCode", typeof(string));

var loRow = loDt.NewRow();
loRow["CategoryName"] = "FirstCategory";
loRow["GroupName"] = "FirstGroup";
loRow["TestName"] = "FirstTest";
loRow["TestNumber"] = "12";
loRow["TestVisitCode"] = "W1";
loDt.Rows.Add(loRow);

loRow = loDt.NewRow();
loRow["CategoryName"] = "FirstCategory";
loRow["GroupName"] = "FirstGroup";
loRow["TestName"] = "FirstTest";
loRow["TestNumber"] = "13";
loRow["TestVisitCode"] = "W2";
loDt.Rows.Add(loRow);

...    

然后我列出所有TestVisitCodes列表:

var loTestVisitCodes = loDt.AsEnumerable().Select(item => item.Field<string>("TestVisitCode")).Distinct();

最后这里是linq查询(查看union语句):

var loCarsCategories = loDt.AsEnumerable()
    .GroupBy(row => new { CategoryName = row.Field<string>("CategoryName") })
    .Select(catGroup => new CarCategory
    {
        CategoryName = catGroup.Key.CategoryName,
        CarGroups = new List<CarGroup>(catGroup
            .GroupBy(row => new { GroupName = row.Field<string>("GroupName") })
            .Select(groupGroup => new CarGroup
            {
                GroupName = groupGroup.Key.GroupName,
                CarTests = new List<CarTest>(groupGroup
                    .GroupBy(row => new { TestName = row.Field<string>("TestName") })
                    .Select(groupTest => new CarTest
                    {
                        TestName = groupTest.Key.TestName,
                        CarTestNumbers = new List<CarTestNumber>(groupTest
                            .Select(row => new CarTestNumber
                            {
                                TestNumber = row.Field<String>("TestNumber"),
                                TestVisitCode = row.Field<String>("TestVisitCode")
                            })
                            .Union(new List<CarTestNumber>(loTestVisitCodes
                                .Except(groupTest.Select(row => row.Field<string>("TestVisitCode")))
                                .Select(VisitCode => new CarTestNumber
                                {
                                    TestNumber = string.Empty,
                                    TestVisitCode = VisitCode
                                }))))
                    }))
            }))
    });