我遇到以下LINQ查询的问题.当嵌套查询(item.Items)有没有对象,我发现了异常"值不能为空参数名:源".
如何获取内部查询以将空列表返回到查询中的项目?
var items = from item in _repository.GetStreamItems()
select new
{
Title = item.Title,
Description = item.Description,
MetaData = item.MetaData,
ItemType = item.ItemType,
Items = from subItem in item.Items
select new
{
Title = subItem.Title,
Description = subItem.Description,
MetaData = subItem.MetaData,
ItemType = subItem.ItemType
}
};
Run Code Online (Sandbox Code Playgroud)
这是使用方法调用而不是查询语法编写的相同查询.同样的问题:
var items = _repository.GetStreamItems()
.Select(x => new { Title = x.Title, Description = x.Description, MetaData = x.MetaData, ItemType = x.ItemType,
Items = x.Items.Select(x2 => new { Title = x2.Title, Description = x2.Description, …Run Code Online (Sandbox Code Playgroud) 我正在创建一个Orchard模块,作为迁移的一部分,我需要创建一个新的内容类型.我能够将一个IContentManager实例注入我的迁移类,这允许我创建新的内容项,但我无法弄清楚如何创建新的内容类型.
有人可以描述这是如何完成的(代码示例会很棒)?
我有一个ASP.NET webform,我使用DropDownList控件允许用户选择一个项目并查看相关结果.出于某种原因,当我设置DropDownList的SelectedValue属性时,它设置的值不会立即可用.
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DropDownList1.SelectedValue = "5";
BindView();
}
}
protected void BindActivities()
{
DataClassesDataContext dc = new DataClassesDataContext();
var query = from activity in dc.Activities
where activity.AssignedTo == Convert.ToInt32(DropDownList1.SelectedValue);
GridView1.DataSource = query;
GridView1.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
在前面的代码中,我得到DropDownList1.SelectedValue为null的错误.奇怪的是,如果我注释掉使用DropDownList1.SelectedValue的代码并让页面加载,DropDownList1实际上设置为值5.所以看起来它设置正确但是不能立即可用.调试器确认DropDownList.SelectedValue在设置它的代码行之后不会立即设置为5.
有什么想法在这里发生了什么?
我正在尝试扩展VS设计器生成的linqtosql类,并需要确定特定字段的值是否已更改.有没有办法让我在表格/实体的DataContext Update方法中访问字段的前后值?
这是我的代码:
public partial class DataClassesDataContext
{
partial void UpdateActivity(Activity instance)
{
this.ExecuteDynamicUpdate(instance);
//need to compare before and after values to determine if instance.AssignedTo value has changed and take action if it has
}
}
Run Code Online (Sandbox Code Playgroud)
我也愿意在Activity实体类中添加一个属性来表示值是否已更改,但我无法弄清楚如何判断值是否已更改.我不能只使用Activity类的OnAssignedToChanged方法,因为只要设置了属性值就会触发,不一定会更改.我正在使用ListView和LINQDataSource控件进行更新,因此无论如何都会设置它.
我还以为我可以使用OnAssignedToChanging方法,但是Activity实例似乎没有当前值.以下代码不起作用.AssignedTo始终为null.
partial void OnAssignedToChanging(int? value)
{
if (value != this.AssignedTo)
{
_reassigned = true;
}
}
Run Code Online (Sandbox Code Playgroud)