3

我在我的 asp.net mvc wen 应用程序中定义了以下模型类:-

public class CustomerDetails
{
    public AccountDefinition AccountDefinition {get; set;}
    public SDOrganization SDOrganization {get; set;}
    public ICollection<SiteDefinition> SiteDefinition { get; set; }
    public ICollection<AaaPostalAddress> AaaPostalAddress { get; set; }
    public ICollection<AaaContactInfo> AaaContactInfo { get; set; }
    public virtual ICollection<UserSiteMapping> UserSiteMapping { get; set; } 
}

然后我编写以下查询:-

var customerDetails = entities.AccountDefinitions
    .Where(a => a.ORG_ID == id)
    .Select(cd => new CustomerDetails
    {
        AccountDefinition = cd,
        SDOrganization = cd.SDOrganization,
        AaaPostalAddress = cd.SDOrganization.AaaPostalAddresses,
        AaaContactInfo = cd.SDOrganization.AaaContactInfoes,
        SiteDefinition = cd.SiteDefinitions,
        UserSiteMapping = cd.SiteDefinitions.Select(p2 => p2.UserSiteMappings) //this is raising the exception
    })
    .SingleOrDefault();

return customerDetails;

但我得到了以下误导性错误:-

无法将类型“System.Collections.Generic.IEnumerable>”隐式转换为“System.Collections.Generic.ICollection”。存在显式转换(您是否缺少演员表?)

我不知道究竟是什么导致了错误,因为我正在选择UserSiteMapping与 SiteDefinition 相关的,并且我将这些分配给 UserSiteMapping 的集合(在我的视图模型中定义)????


更改库 DhtmlX 中的视图

我想使用这个库DHTMLX:我想使用这个视图:时间线视图 所以我将我的视图更改index.cshtml为这个:

<!DOCTYPE html>
<html>
<head>
    <title>DHXScheduler initialization sample</title>
    <script src="~/Scripts/dhtmlxScheduler/ext/dhtmlxscheduler_timeline.js" ></script>
    <script src="~/Scripts/dhtmlxScheduler/ext/dhtmlxscheduler_treetimeline.js" ></script>
    <script>
        scheduler.locale.labels.timeline_tab = "Timeline";
        scheduler.createTimelineView({
            name: "timeline",
            x_unit: "minute",//measuring unit of the X-Axis.
            x_date: "%H:%i", //date format of the X-Axis
            x_step: 30,      //X-Axis step in 'x_unit's
            x_size: 24,      //X-Axis length specified as the total number of 'x_step's
            x_start: 16,     //X-Axis offset in 'x_unit's
            x_length: 48,    //number of 'x_step's that will be scrolled at a time
            y_unit:         //sections of the view (titles of Y-Axis)
               [{ key: 1, label: "Section A" },
                { key: 2, label: "Section B" },
                { key: 3, label: "Section C" },
                { key: 4, label: "Section D" }],
            y_property: "section_id", //mapped data property
            render: "bar"             //view mode
        });
</script>
    <style>
        body
        {
            background-color:#eee;
        }
    </style>
</head>
<body>
    <div style="height:700px;width:900px;margin:0 auto">
       @Html.Raw(Model.Render())
         </div>
    <div id="scheduler_here" class="dhx_cal_container" >
    <div class="dhx_cal_navline">
    <div class="dhx_cal_tab" name="timeline_tab" style="right:280px;">
     </div>
    </div>
    </div>
    
</body>
</html>

结果我想看到这个视图结果1

但我有这样的看法:结果2

那么片段中的问题是什么?我怎样才能改变它以获得好的结果?

4

1 回答 1

5

改用SelectMany

UserSiteMapping = cd.SiteDefinitions.SelectMany(p2 => p2.UserSiteMappings)

您可能还需要ToList()ToArray()致电以切换IEnumerable<T>ICollection<T>

UserSiteMapping = cd.SiteDefinitions.SelectMany(p2 => p2.UserSiteMappings).ToList()
于 2013-07-09T09:45:45.527 回答