English 中文(简体)
d之间是否存在任何区别。 类别和清单
原标题:Is there any difference between db.Categories and List<Category>

下述法典行之有效。

IEnumerable<GroupedSelectListItem> groupList = db.Categories.Select(p => new GroupedSelectListItem()
{
     GroupKey = p.ParentCategory.Name,
     GroupName = p.ParentCategory.Name,
     Text = p.Name,
     Value = p.Name
});

下面的代码产生,但并未提及某一实例错误

List<Category> orderedList = new List<Category>();
var rootList = db.Categories.Where(c => c.ParentCategoryId == null).ToList();
    foreach (var item in rootList)
    {
        orderedList.Add(item);
        if (item.SubCategories.Count != 0)
        {
            foreach (var subcat in item.SubCategories)
            {
                orderedList.Add(subcat);
                if (subcat.SubCategories.Count != 0)
                {
                    foreach (var subsubcat in subcat.SubCategories)
                    {
                        orderedList.Add(subsubcat);
                    }
                }
            }
        }
    }
IEnumerable<GroupedSelectListItem> groupList = orderedList.Select(p => new GroupedSelectListItem()
{
     GroupKey = p.ParentCategory.Name,
     GroupName = p.ParentCategory.Name,
     Text = p.Name,
     Value = p.Name
});

www.un.org/Depts/DGACM/index_spanish.htm Error Detail

第54条:数字群体List = 命令List.Select(p =>新集团SelectListItem()

<>光> 数据库的类别表包含一些与父母关系的内容,这些内容涉及某些记录,其价值等于零。

最佳回答

The first code snippet is using Linq-to-entities with automatic coalescence. It menas that if p doesn t have ParentCategory it will work without problem on SQL level. The second example use Linq-to-objects where no such feature exists. You must manually check if ParentCategory property is filled prior to accessing its properites:

GroupKey = p.ParentCategory != null ? p.ParentCategory.Name : null 
问题回答

暂无回答




相关问题
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. ...

热门标签