简体   繁体   English

LINQ连接两个表并返回列表

[英]LINQ join two tables and return list

Update: In the comments it was made clear to me where I was getting confused, so although I can't mark a comment as an answer, this question has been solved. 更新:在评论中,我很清楚我感到困惑的地方,因此尽管我无法将评论标记为答案,但此问题已解决。 In reality everything was working fine when I used the ViewModel, I just wasn't using that properly in the view. 实际上,当我使用ViewModel时,一切工作正常,只是在视图中未正确使用它。 Thanks for your help! 谢谢你的帮助!

I'm building a site in MVC6. 我正在MVC6中建立一个站点。 I'm trying to create a linq statement to join the booking and venue tables together, then return any bookings still forthcoming. 我正在尝试创建一个linq语句以将预订表和场所表连接在一起,然后返回仍在进行的所有预订。 I'm happy that my from and join statements are good, as well as my filter. 我很高兴我的from和join语句以及过滤器都很好。

I have the following code: 我有以下代码:

private readonly ApplicationDbContext ctx;

public ForthcomingBookingsViewComponent(ApplicationDbContext c)
{
    ctx = c;
}

public IViewComponentResult Invoke(string filter)
{
    DateTime todaysDate = new DateTime();
    todaysDate = DateTime.Now;

    var res = (from p in ctx.Booking
               join i in ctx.Venue on p.VenueId equals i.VenueId
               where p.DonorId.Equals(filter)
               where p.BookingDate >= todaysDate
               select    )  //This is the line I'm struggling to work out
               .toList();
    return View(res);
}

The trouble comes in trying to get this in a list that I can refer to in my view. 麻烦在于试图将其包含在我认为可以引用的列表中。 I've tried a couple of ways of doing this, first of all I tries to do the following: 我尝试了几种方法,首先,我尝试执行以下操作:

select new { p.BookingDate, i.VenueName }

But the problem there is that I'm not sure how to refer to this as a model in my view. 但是问题在于我不确定如何将其称为模型。

I then thought I might use a ViewModel, so I could define what goes in it first: 然后我以为可以使用ViewModel,因此可以先定义其中的内容:

   select new FCBViewModel { BookingDate = p.BookingDate, VenueName = i.VenueName })
   .ToList();

But once again I had trouble because this sets it as a single object rather than a list. 但是我又遇到了麻烦,因为这将它设置为单个对象而不是列表。 To formalize the question: 正式化问题:

How do I return a list of objects, from two joined tables, that I can use in my view? 如何从两个联接的表中返回可以在视图中使用的对象列表?

Thanks! 谢谢!

You can do a projection in your LINQ expression using your view model class. 您可以使用视图模型类在LINQ表达式中进行投影。

public IViewComponentResult Invoke(string filter)
{
    DateTime todaysDate = new DateTime();
    todaysDate = DateTime.Now;

    var fcbVmList = (from p in ctx.Booking
               join i in ctx.Venue on p.VenueId equals i.VenueId
               where p.DonorId.Equals(filter)
               where p.BookingDate >= todaysDate
               select new FCBViewModel { BookingDate = p.BookingDate,
                                         VenueName = i.VenueName })
               .toList();
    return View(fcbVmList);
}

In the above code, the variable fcbVmList will be a List of FCBViewModel and you are passing that to your view component's view. 在上面的代码中,变量fcbVmList将是一个List FCBViewModel和你逝去,为您的视图组件的视图。 Now this list contains only one item or n items is depending on your where clause and your data in the tables. 现在,此列表仅包含一项或n项,这取决于您的where子句和表中的数据。 But it is returning a List. 但是它正在返回一个列表。 So make sure that your view is strongly typed to a collection of this class. 因此,请确保将您的视图强类型化为此类的集合。

@model List<FCBViewModel>
<h2> @Model.Count() Bookings </h2>
@foreach(var item in Model)
{
  <p>@item.VenueName</p>
  <p>@item.BookingDate</p>
}

Did you try like this. 你这样尝试过吗

      var resultMBSSize = (from obj1 in objname.GetAll()
                           join obj2 in objname2.GetAll()
                           on obj1.id equals
                           obj2.id
                           where obj1.brandID== argumentvalue
                           select new objname
                           {
                           id=obj1.ModifierBrandSelectionSizeID,
                           name = obj2.Name,
                           price = obj1.Price,
                           address = obj2.address
                                         }).ToList()

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

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