简体   繁体   English

LINQ:在linq中使用数据透视表

[英]LINQ: Using a pivot table in linq

Can anyone help? 有人可以帮忙吗?

I have the following structure using associations, and as you can see v.StructureType.Description works great as its a 1 to 1 association, but i need to get in my example below v.StructureGroup.StructureGroupTariffs.Tariff.Price but StructureGroupTariffs is a Pivot table to interlink the StructureGroup and Tariffs, hence its a Many to Many so it returns a EntitySet, I understand the issue now but i am unsure how to fix it 我有以下使用关联的结构,如您所见,v.StructureType.Description的一对一关联效果很好,但是我需要在下面的示例中获取v.StructureGroup.StructureGroupTariffs.Tariff.Price,但StructureGroupTariffs是一个数据透视表将StructureGroup和Tariffs相互链接,因此它是“多对多”的,因此它返回EntitySet,我现在了解此问题,但不确定如何解决

    from v in Houses
       select new 
       {
          Id = v.Id,
          Color = v.Color.Description,
          StructureType= v.StructureType.Description,
          Price = v.StructureGroup.StructureGroupTariffs. // Tariff.Price doesn't appear because its MANY to ONE
       }

So understanding that my structureGroupTariffs is my pivot (inter linking table) i must pass in 因此,理解我的structureGroupTariffs是我的枢纽(内部链接表)后,我必须传递

                 IdTariff  and  StructureGroupId

this then gives me a 1 to 1... So i presume i can then use StructureGroupTariffs.Tariff.Price ??? 然后这给了我1比1 ...所以我想我可以使用StructureGroupTariffs.Tariff.Price ??? as this will then return a 1 to 1 between StructureGroup and Tariff 因为这将在StructureGroup和Tariff之间返回1到1

I am a little confused and would really appreciate some help. 我有些困惑,非常感谢您的帮助。

I understand the issue now but unsure of how i can fix it. 我现在了解此问题,但不确定如何解决。

Think about how you would solve this kind of query in SQL and I think you can apply that reasoning. 考虑一下如何解决SQL中的这种查询,我认为您可以应用这种推理。 There are two primary ways of writing this kind of join: the first is to use inner joins from the main table to the join table and again to the result table, with appropriate filters in place. 编写这种连接的主要方法有两种:第一种是使用从主表到连接表的内部连接,再到结果表,并使用适当的过滤器。 If you are not careful to filter the join correctly you would get multiple rows per main table (which is effectively why you are getting an EntitySet. If you use .FirstOrDefault() against that entity set you will get the Entity you are looking for (assuming that you have filtered down to a single result: if not you will get some random result). 如果您不小心正确地过滤联接,则每个主表将获得多行(这实际上就是为什么要获得EntitySet的原因。如果对该实体集使用.FirstOrDefault(),则会得到您要查找的Entity(假设您已过滤到单个结果:如果没有,您将获得一些随机结果)。

An alternative would be to write a subquery, such as 一种替代方法是编写一个子查询,例如

Price = (from t in v.StructureGroup.StructureGroupTariffs.Tariffs 
where t.StructionGroupID = v.StructureGroupID 
and t.IdTariff = *not sure where this filter comes from*
select t.Price).First()

The missing filter above is likewise a problem for the first approach: either way you need to uniquely identify the t.Price you want. 上面缺少的过滤器同样是第一种方法的问题:无论哪种方式,您都需要唯一地标识所需的t.Price。

Here is an example of inner joins (simplified from a schema of mine where Docs are many to many with DocTags via the Doc_DocTag table. 这是一个内部联接的示例(从我的模式简化而来,其中通过Doc_DocTag表使用DocTag的文档数很多。

from d in Docs 
join ddt in Doc_DocTags on d.DocGuid equals ddt.DocGuid
join dt in DocTags on ddt.DocTagID equals dt.DocTagID
select new {
    d.DocName, dt.DocTagName
}

If I wanted only one tag: 如果我只想要一个标签:

from d in Docs 
join ddt in Doc_DocTags on d.DocGuid equals ddt.DocGuid
join dt in DocTags on ddt.DocTagID equals dt.DocTagID
where dt.DocTagName == "Target"
select new {
    d.DocName, dt.DocTagName
}

This would find only docs with a tag of Target (which would further ensure that multiple DocTags are not possible). 这样只会找到带有目标标签的文档(这将进一步确保不可能有多个DocTag)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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