English 中文(简体)
我如何掌握所有要素,如果他们被分配?
原标题:How do I get all the elements and if they are assigned?

I have the following model.

Subscription    Packages    PackageWidgets    Widgets
------------    --------    --------------    -------
ID              ID        < PackageID         ID
PackageID    >              WidgetID       >

我正在使用实体框架4和<代码>Subscription与Package的关系。 <代码>Package与<代码>Widgets清单的关系。

我在使用Linq时试图获得一份allWidgets清单,如果将其列入目前的订阅。 也许,这要归功于我的卡片背景,即我看不到林克的问候。 结构将包括一个来自温得植被的SlectT,一个LEFT JOIN,通过一个基于在子保单中通过的内容的子描述、包裹和包装产品。

The output I would expect would be something like WidgetID,IsIncluded such that I would have all Widget IDs and a boolean indicating the inclusion status.

我甚至似乎甚至无法稍微接近工作,以显示我迄今所做的工作。

没有人能够就如何完成我的询问给我一些见解?

Update: Here is what I ve come close with, but it still doesn t work. Maybe it will help illustrate what I am trying to accomplish though:

from subscription in Subscriptions
where subscription.ID == 3
let subWidgets = subscription.Package.Widgets
from widget in Widgets
join subWidget in subWidgets on widget.ID equals subWidget.ID into joined
from list in joined.DefaultIfEmpty()
select new {
    ID = widget.ID
    ,Selected = subWidget.ID != null
}

Update #2 Thanks to the accepted answer, this is what I ended up going with - which does what I need:

from widget in Widgets
from subWidgets in
  from subscription in Subscriptions
  where subscription.ID == 3
  select subscription.Package.Widgets
orderby widget.ID
select new { 
    Name = widget.WidgetName,
  Available = subWidgets.Contains(widget)
}

Thanks for the assist!

最佳回答

解决这一问题的一个途径是打破僵局,例如:

var widgetsInSubscription =
    from subscription in Subscriptions
    where subscription.ID == 3
    from widget in subscription.Package.Widgets
    select widget;

var allWidgets =
    from widget in Widgets
    select new
    {
        widget.ID,
        Selected = widgetsInSubscription.Contains(widget),
    };

或者根据国际发展法而不是物体进行,例如:

var widgetIDsInSubscription =
    from subscription in Subscriptions
    where subscription.ID == 3
    from widget in subscription.Package.Widgets
    select widget.ID;

var allWidgets =
    from widget in Widgets
    select new
    {
        widget.ID,
        Selected = widgetIDsInSubscription .Contains(widget.ID),
    };

铭记如果你愿意的话,你总是能够做问答,在EF4中,你只能打电话

问题回答

类似:

from s in db.Subscriptions
from p in db.Packages
from pw in db.PackageWidgets
from w in db.Widgets
where w.ID == pw.WidgetID &&
      pw.PackageID == p.ID &&
      s.PackageID == p.ID
select w;

Dunno, 如果有的话。 然而,如果你拥有像我的Package这样的财产。 说明等,这或许可以简化很多。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签