在EntityFramework中获取表数据 [英] Getting the Table data in EntityFramework

查看:231
本文介绍了在EntityFramework中获取表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..我正在使用2个表人和国家。





姓名 - 职业 - Countryid

马克 - 销售人员--1003

William - 开发人员 - 1001



国家

id - 姓名

1001 - 圣弗朗西斯科

1002 - 拉斯维加斯

1003 - 纽约



这里的人参考国家(国家ID)。考虑这些类已经通过EntityFramework自动生成,我将国家作为类名称推荐给受尊重的表。



现在我通过以下内容获取人员数据

Hi.. I am using 2 Tables say Person and Country.

Person
Name--Occupation--Countryid
Mark--Sales Man --1003
William--Developer--1001

Country
id--Name
1001--San Fransisco
1002--Las Vegas
1003--New York

Here the Person references Country (country id). Consider the classes are already auto generated through EntityFramework and I have Persons and Countries as class name to the respected tables.

Now I am getting the Persons Data thorough the following

this.ObjectContext.Persons.Where(x=>x.Countryid==1003).ToList<Persons>();





这实际上返回PersonsList但是PersonsList [0] .Country(例如:我在这里得到列表的第一个元素)在这种情况下将为null。



所以这里我使用包含上述代码的



This actually returns PersonsList but PersonsList[0].Country (For example: I am getting the first element of the list here) would be null at this case.

So here I am using Includes with the above code

this.ObjectContext.Persons.Includes("Country").Where(x=>x.Countryid==1003).ToList<Persons>();





现在PersonsList [0] .Country将有da也适用于该行所引用的Country类的ta。



我的问题是..是否有任何选项我可以获得此包含类的国家数据。 ?



因为根据我的情况,如果我使用这个Include类,则需要2-3分钟(因为数据庞大)来处理查询。这主要是因为EntityFramework生成的查询还有一个Country with Persons的LeftOuterJoin。



如果我没有使用Include,则只需几秒钟即可处理查询。因为这里跳过了LeftOuterJoin。所以无论如何都是这样我可以访问Country类的数据而不包括.. ??





我尽力解释这个问题。如果你不能得到这个PLZ随时问我怀疑..



谢谢..



Now PersonsList[0].Country will have the data for the Country class which that person row references too.

My question is.. Are there any options where I could get this Country data with out this Includes class. ?

Because under my case if I am using this Include class it tooks 2-3 mins (because of the huge data) to process the query. This is mainly because the query generated by EntityFramework is having one more LeftOuterJoin of Country with Persons.

If I am not using the Include then it take only few seconds to process the query. Because here the LeftOuterJoin is skipped. So is there is anyway so that I can access the data of the Country class with out Include.. ??


I have tried my level best to explain this. If you couldn't get this plz feel free to ask me your doubt..

Thanks..

推荐答案

If you want to join both the table and to get the data as listing type

var listNew=(from a in Countries
             join b in Persons on a.CountryId equals b.CountryId
             select new {
             CountryName=a.Name,
             PersonName=b.PersonName
              }).Tolist();

Something like this


这篇关于在EntityFramework中获取表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆