简体   繁体   English

LINQ关联-实体框架

[英]LINQ association - Entity Framework

I have three tables and I have used edmx designer to add associations between them. 我有三个表,并且我已经使用edmx设计器在它们之间添加关联。 Below is how they are linked. 下面是它们的链接方式。

(table1) Loans - (table 2) Investor : Many to One relationship (Table2) Investor - (Table3) InvestorInfo : One to Many relationship (表1)贷款-(表2)投资者:多对一关系(表2)投资者-(表3)InvestorInfo:一对多关系

I want to get [1] Total loans count sold to one investor, [2] Investor name and [3] investor's service fee which is stored in Table3 at idx = 2005 for each investor ("investor id & idx" is primary key of table3 - InvestorInfo table). 我想得到[1]卖给一位投资者的贷款总数,[2]投资者名称和[3]投资者的服务费,这些费用存储在表3中,每个投资者的idx = 2005(“ investor id&idx”是table3-InvestorInfo表)。

How do I do that in below query? 我如何在下面的查询中做到这一点? I am forced to select 'FirstOrDefault()' to access any column in Table3 (See commented lines). 我被迫选择“ FirstOrDefault()”来访问表3中的任何列(请参见注释行)。 If I use FirstOrDefualt, I get a record where idx = 1 and not 2005. 如果使用FirstOrDefualt,则会得到idx = 1而不是2005的记录。

 var loanPurchaseData = (from cd in entity.Table1
                        //where cd.Table2.Table3.Select(x => x.IDX == 2005)
                        //where cd.ULDD_SET_POOLS.ULDD_SET_POOLDT.FirstOrDefault().SORT_ID == 2005
                        group cd by new { cd.Table4.PurchaseDate, cd.Number } into grp
                        select new
                        {
                            investor = grp.FirstOrDefault().Investor,
                            no_of_loans = grp.Count(),                                               
                            sort_id = grp.FirstOrDefault().Table2.Table3.FirstOrDefault().SORT_ID,
                            service_fee_rate = grp.FirstOrDefault().Table2.Table3.FirstOrDefault().DT_REAL_PERC_VALUE
                        }).ToList();

Your question is not very clear - I don't understand whether idx is in Table3 or Table1, and what you want to select, but I will assume you have a many-one-many schema, where an Investor has zero or more Loans & zero or more InvestorInfos . 您的问题不是很清楚-我不知道idx是在Table3还是在Table1中,以及您要选择什么,但是我假设您有一个多对多的架构,其中Investor有零个或多个Loans ,零个或多个InvestorInfos You want to get, say, all of the loans which join to investor info with idx = 2005 . 例如,您想获得所有与idx = 2005投资者信息有关的贷款。 Correct me if I'm wrong, and correct your question if I'm right! 如果我错了,请纠正我,如果我错了,请纠正您的问题!

Starting with your InvestorInfo object, you know that there's only one Investor but there will be zero or more Loans . InvestorInfo对象开始,您知道只有一个Investor但是会有零个或多个Loans

// only one InvestorInfo for idx, but this isn't clear in your question
var investorInfo = context.InvestorInfos.SingleOrDefault(i => i.idx == 2005);

var loans = investorInfo.Investor.Loans;

The crux of your problem is that you cannot get 'the loan service fee' for an investor info. 问题的症结在于,您无法获得投资者信息的“贷款服务费”。 Why not? 为什么不? Because that investor has 5 loans. 因为那个投资者有5笔贷款。 Which one do you want? 你想要哪一个?

-- we can get the maximum, minimum, sum, etc...
var max = loans.Max(l => l.DT_REAL_PERC_VALUE);
var min = loans.Min(l => l.DT_REAL_PERC_VALUE);
var min = loans.Sum(l => l.DT_REAL_PERC_VALUE);

Again it's not clear what you are trying to do, nor what your data actually looks like, but in a one-many relationship you will necessarily have more than one of the 'many' side for each of the 'one' side. 同样,也不清楚您要做什么,也不清楚数据的实际外观,但是在一对多关系中,对于“一个”一侧的每个,您必然会拥有多个“一个”一侧。


To get the max, use the Max operator. 要获得最大值,请使用Max运算符。

service_fee = grp.Max(l => l.Table2.Table3.Max(t => t.DT_REAL_PERC_VALUE)) 

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

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