Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq到实体表联接/SQL表联接-ASP.NET_C#_Asp.net_Linq To Entities - Fatal编程技术网

C# Linq到实体表联接/SQL表联接-ASP.NET

C# Linq到实体表联接/SQL表联接-ASP.NET,c#,asp.net,linq-to-entities,C#,Asp.net,Linq To Entities,我把这张桌子摆好了: tblOne-一条/唯一记录 personID主键 菲尔德一号 tblTwo-Many 拟人外键 第二场 其他人 t树-许多 拟人外键 第三场 tblFour-许多 拟人外键 第四场 代码隐藏中的代码: var tmp = textbox.Text; var entity = new Entities(); var base = entity.tblTWO.Where(x => x.otherID == tmp) .Se

我把这张桌子摆好了:

tblOne-一条/唯一记录

personID主键 菲尔德一号 tblTwo-Many

拟人外键 第二场 其他人 t树-许多

拟人外键 第三场 tblFour-许多

拟人外键 第四场 代码隐藏中的代码:

var tmp = textbox.Text;
var entity = new Entities();
var base = entity.tblTWO.Where(x => x.otherID == tmp)
                        .Select(x => new
                          {
                             otherID = tmp,
                             personID = x.personID,
                             fieldOne = x.tblONE.fieldOne
                            //Need fields from tblThree and tblFour
                          })
                        .ToList();
理论上,我希望能够搜索otherID并从所有4个表中提取所有相关字段

所以,如果我在otherID=123上搜索,并且该记录的personID=999,那么我将从tblOne、tblTwo和tbltree中提取personID=999的所有数据

我正在使用Linq对代码隐藏中的实体进行访问,只在搜索tblTwo和从tblOne获取相关字段方面有所进展。然后我了解到我可以走多对一的路线,但不能走相反的路线,因此当我试图从tblOne追踪到tblThree或tblFour时,我会被卡住

我试图拉取这些字段,稍后将它们绑定到单独的控件,以便在web应用程序中显示记录,即gridview中字段1的所有唯一记录。Linq到实体不是正确的方法吗?我认识的人提到尝试使用SQL,但我不确定如何正确地连接所有这些表以获得我所需要的


任何帮助都将不胜感激!!!非常感谢

为什么不匹配表2中的OtherID,检索PersonID

并使用新的PersonID变量执行Linq表达式,以从其他表检索必要的数据,假设它们共享相同的PersonID,这就是SQL设置中的情况

var pID = entity.tblTWO.Where(x => x.Other_ID == tmp).Select(x => x.personID).toString(); //This retrieves the personID from table2, which you will use to search the other tables
var list = entity."sometable".Where(y => y.personID == pID).Select("Data").toList();     //Using the above PersonID, you search any table you want for a match and retrieve any data you need using "Select"
编辑: 如果您正在谈论表联接,请参阅一些SQL语句。首先,您必须考虑是否需要将这些表分开,如果每个表根据每个personID包含相似的信息,我建议加入这些表

我不清楚你在找什么? 在代码中,要搜索的临时ID来自文本框。
因此,您接受此用户输入,首先在表2中搜索匹配的otherID,在找到匹配项后,您现在从该表中检索personID,该personID应使用此personID在每个表中共享,您现在可以搜索剩余表并提取数据并按需存储。

根据您的表详细信息,由于tbltree和tblFour是tblOne的1对多,这些表中的字段是集合而不是单个实体,因此有不同的访问方式:

从您详细描述的方式来看,它应该是一个集合:

.Select(x => new
{
    MyCollection = x.tblOne.tblThree.Select(t=> new MyObject(t)).ToArray()
}
如果您只得到第一个值注释,如果personID不在tblThree中,则会失败:

.Select(x => new
{
    firstFieldThree = x.tblOne.tblThree.First().fieldThree
}
如果tbltree和tblFour中确实有一个值,那么personID应该是主键和外键,那么您可以像这样访问它们:

.Select(x => new
{
    fieldThree = x.tblOne.tblThree.fieldThree
}
var base = from t1 in entity.t1
           from t2 in t1.t2.DefaultIfEmpty()
           from t3 in t1.t3.DefaultIfEmpty()
           from t4 in t1.t4.DefaultIfEmpty()
           where t2,otherID == tmp
           select new
           {
             otherID = tmp,
             t2.personID,
             t1.fieldOne,
             t3.fieldThree,
             t4.fieldFour
           };

您需要将所有表连接在一起。如果需要左联接,请使用DefaultIfEmpty。您可以像这样使用实体1导航属性:

.Select(x => new
{
    fieldThree = x.tblOne.tblThree.fieldThree
}
var base = from t1 in entity.t1
           from t2 in t1.t2.DefaultIfEmpty()
           from t3 in t1.t3.DefaultIfEmpty()
           from t4 in t1.t4.DefaultIfEmpty()
           where t2,otherID == tmp
           select new
           {
             otherID = tmp,
             t2.personID,
             t1.fieldOne,
             t3.fieldThree,
             t4.fieldFour
           };
您还可以连接四个表-

var base = (from t1 in entity.tblOne
                     join t2 in entity.tblTwo on t1.personID equals t2.personID
                     join t3 in entity.tblThree on t1.personID equals t3.personID
                     join t4 in entity.tblFour on t1.personID equals t4.personID
                     where t2.otherID == temp
                     select new{
                         OtherId = temp,
                         PersonId = t1.personID,
                         FieldOne = t1.fieldOne,
                         FieldThree = t3.fieldThree,
                         FieldFour = t4.fieldFour
                     }).ToList();

如何将personID匹配的数据连接到otherID匹配的数据?我还需要otherID所在的tblTwo中的字段,并且我需要otherID的每个记录都包含所有相关的personID信息(如果有意义的话)。那么,我猜是一个表连接。。。?对不起,这里是全新的。不过,谢谢你的帮助!很抱歉,不够清晰。当我使用Linq时,tblOne与其他表有一对多的关系。所以,在使用你的建议时,当我尝试做。。。例如:var list=blahblah.Selectx=>new{fieldFour=x.tblFour.fieldFour}.ToList;它无法从tblFour中识别字段4。tblFour的字段都没有显示在intellisense中。这有意义吗?因此,我无法使用Linq选择这些字段。是的,您是对的,表1具有一对多关系。使用Linq时,您应该能够看到x.tblFour.dataInTblfour。确保您没有从外键中选择实际的int key值,而是实际选择了整个对象。我知道,当我使用VS2013时,一些intellisense不起作用。您可能希望将IDE更新到最新版本,并确保启用intellisense我知道它可以工作,下面是我的一个项目中的一个示例,其中Status1是一个表,但Tasks有一个对状态表ID的外键引用。您还可以看到我如何使用一对多关系检索状态的名称。viewModel.Tasks=db.Tasks.Wheres=>s.Status1.Name==Open.ToList;非常感谢。这正是我所需要的:不过,我还有一个问题。假设表t4有一个名为City的字段,这些值是一个单独的表tblCities的ID编号,该表具有实际的城市名称。我该如何加入其中以检索城市名称?非常感谢你的帮助!!安德,好的。我能够在代码中显示不同表的字段,但是当我将数据绑定到控件时,数据没有显示出来。威尔骗局
继续研究/调试。'@Noobody也许你需要做一个左连接。我添加了DefaultIfEmpty,也许这将有助于感谢您的输入!快速提问-如果t4中没有匹配的personID数据。。。此查询是否为空?我想我是在问。。。这是否需要在所有表中都有一个personID匹配才能工作?抱歉,响应太晚。是的,personID需要在四个表中匹配。如果不匹配,结果将为空。