C#LINQ查询来自多个表的数据

时间:2018-07-03 10:18:45

标签: c# linq join

我有2张桌子:

Metrics(id,指标):[{1,Metric1},{2,Metric2},{3,Metric3}]

id | Metric
------------------------------------------------------------
1  |  Metric1
2  |  Metric2
3  |  Metric3

MetricsValuePerPerson(id,PersonId,Metricid,Value)

id | PersonId | Metricid | Value
------------------------------------------------------------
1  |  1       |     1    |  P1metric1Value
2  |  1       |     2    |  P1Metric2Value
3  |  1       |     3    |  P1metric3Value
4  |  2       |     1    |  P2Metric1Value
5  |  2       |     2    |  P2metric2Value
6  |  2       |     3    |  P2Metric3Value

我想在网格中显示值,其中每一行是Person,每一列是Metric

PersonId | Metric1         | Metric2        | Metric3
------------------------------------------------------------
P1       |  P1metric1Value | P1Metric2Value | P1Metric3Value
P2       |  P2metric1Value | P2Metric2Value | P2Metric3Value

这是我尝试加载商店的方式:

var metricValues = from mv in dc.MetricsValuePerPerson
                   join m in dc.Metrics on mv.Metricid equals m.id
                   select new { Personid = mv.Personid, Metric1 = (codesnippet) , Metric2 = (codesnippet), Metric3 = (codesnippet) };

如何替换(codesnippet)以获得Value的{​​{1}}?

2 个答案:

答案 0 :(得分:0)

在LINQ中,只要您已在数据库中建立关系,就很少需要使用JOIN关键字。在您的情况下,在数据库中您具有MetricsValuesPerPerson和Metrics之间的关系,对吗?如果是这样,则:

var metricValues = from mv in dc.MetricsValuePerPerson
                   select new { 
                    Personid = mv.Personid, 
                    Metric1 = mv.Metric.Metric };

结果似乎并不有意义(可能您也想添加Value)。

编辑:这就像一个枢轴,假设您只有3个度量标准,并且每个人的每种度量标准类型最多1个(即:没有personId,metricId,(1,1,10)和( 1,1,20)。

var result = from mv in MetricsValuePerPersons.Where(mvpp => mvpp.Metricid == 1)
                 let mv2 = MetricsValuePerPersons.SingleOrDefault(mvpp => mvpp.PersonId == mv.PersonId && mvpp.Metricid == 2)
                 let mv3 = MetricsValuePerPersons.SingleOrDefault(mvpp => mvpp.PersonId == mv.PersonId && mvpp.Metricid == 3)
                 select new {
        PersonId = mv.PersonId,
        Metric1 = mv.Value,
        Metric2 = mv2.Value,
        Metric3 = mv3.Value,
    };

答案 1 :(得分:0)

您可以使用GroupJoin(相当于SQL中的LEFT JOIN)来执行此操作。将MetricsValuePerPersonMetrics连接起来,然后将GroupBy插入PersonId,然后将Select连接到结果。示例代码在Lambda Join中,但是您可以将其转换为LINQ

工作示例:C# Fiddle

var result = metricValuePerPerson.GroupJoin(metrics, mv => new { Id = mv.PersonId }, m => new { Id = m.Id }, 
        (mv, m) => new { mv = mv, m = m })
        .SelectMany(x => x.m.DefaultIfEmpty(), (x, y) => new {mv = x.mv, m = y })
        .GroupBy(g => g.mv.PersonId)
        .Select(x =>  
        {
           var grouped = x.ToList();
           return new 
            {
                PersonId = x.Key, //The below are sample code, please use your actual code.
                Metric1 = grouped[0].mv.PersonId + "|" + grouped[0].m.Metric + "|" + grouped[0].mv.Value,
                Metric2 = grouped[1].mv.PersonId + "|" + grouped[1].m.Metric + "|" + grouped[1].mv.Value,
                Metric3 = grouped[2].mv.PersonId + "|" + grouped[2].m.Metric + "|" + grouped[2].mv.Value,
            };
        })
        .ToList();