C# 带有实体框架的 Linq

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18617058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:45:58  来源:igfitidea点击:

Linq with entity framework

c#linqentity-framework

提问by razeth01

I'm new to LINQ and Entity Framework and would appreciate some advice on the following scenario.

我是 LINQ 和实体框架的新手,希望得到有关以下场景的一些建议。

I have a entity model with two tables.

我有一个带有两个表的实体模型。

Travel_Request and Resource

Travel_Request 和资源

Sample Fields

示例字段

Travel_Request

Travel_Request

Request_ID
Resource_ID

Resource

资源

Resource_ID
Resource_Name

I would like to add the Resource_Name to the list when returning all the TRAVEL_REQUESTS

我想在返回所有 TRAVEL_REQUESTS 时将 Resource_Name 添加到列表中

Thanks in advance

提前致谢

采纳答案by Fabien

Hi you need to use the Linq join:

您好,您需要使用 Linq 连接:

var data = from t in Travel_Request
       join r in Resource on t.Resource_ID equals r.Resource_ID
       select new 
              { 
                 RequestId = t.Request_ID,
                 ResourceId = t.Resource_ID,
                 ResourceName = r.Resource_Name
              };

If you already have an EF association then it could simply be:

如果您已经有一个 EF 关联,那么它可能只是:

var data = from t in Travel_Request
    select new
           {
                 RequestId = t.Request_ID,
                 ResourceId = t.Resource_ID,
                 ResourceName = t.Resource.Resource_Name
           };

回答by Rob White

You will have to create a new object something like this.

您将不得不创建一个类似这样的新对象。

var data = Travel_Request.Select(s=> new { Resource_Name = s.Recource.Resource_Name, Request_ID = s.Request_ID, Resource_ID = s.Resource_ID}).ToList();

As long as I've understood the question correctly this will work.

只要我正确理解了这个问题,这就会起作用。

回答by Thilina H

You can query Travel_Request entity and use the navigation propety to do that.

您可以查询 Travel_Request 实体并使用导航属性来执行此操作。

var resultList = DbContext.Travel_Request.Resources.where(x=>x.Resource_Name =="Your string").ToList();

回答by Michael90

Use the Join:

使用连接:

Travel_Request.Join(Resource, tr => tr.ResourceId, r => r.ResourceId, (tr, r) => new { RequestId = tr.RequestId, ResourceId = tr.ResourceId, Name = r.ResourceName})

Or what a better practice would be is adding a Navigation property to TravelRequest. TravelRequest would look like this:

或者更好的做法是向 TravelRequest 添加 Navigation 属性。TravelRequest 看起来像这样:

int RequestId { get; set; }
int ResourceId{ get; set; }
[ForeignKey("ResourceId")]
Resource Resource { get; set; }

This way you can use Include

这样你就可以使用 Include

DBContext.TravelRequests.Include("Resource").First().Resource.Name;

Include tells EF to get the Resource with the TravelerRequest

Include 告诉 EF 使用 TravelerRequest 获取资源

回答by Ryan Schlueter

I would add a model that has all three then do something like.

我会添加一个包含所有三个的模型然后做类似的事情。

var TravelRequests = 
    from p in Travel_Request
from r in Resource.where(Res => Res.Resource_ID == p.Resource_ID)
    select new TravelModel{
requestID = p.requestID,
Resource_ID = p.Resource_ID,
ResourceName = r.ResourceName

}; 

But thats just an example of how I would do it and there might be better ways. Also syntax might be a little off but general idea is there.

但这只是我将如何做的一个例子,可能有更好的方法。语法也可能有点偏差,但总体思路是存在的。