数码幽灵的自学Blog

.Net 学习历程

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
最近在使用Base4.Nethttp://www.base4.net开发一个台帐管理系统,定义了一个类NoteCategory

public class NoteCategory
{
    
public ItemCollection Parents; //Target is source
    
}

大致的Xml Schema如下

<!-- 定义表单模版类型实体 -->
    
<TypeDef fullName="DGSoft.NoteManagementAlpha2.Data.NoteCategory" baseName="Maxtiviti.Storage.ItemBase">
        
<PropertyDef name="Title" type="string" length="100" nullable="false" />
        
<RelationshipDef name="Members" type="Maxtiviti.Storage.ItemBase" relationshipName="NoteCategoryMembers" direction="LinkIsSource" />
        
<PropertyDef name="Copyright" type="string" length="200" nullable="true" />
        
<PropertyDef name="Description" type="string" length="1000" nullable="true" />
        
<RelationshipDef name="Parents" type="DGSoft.NoteManagementAlpha2.Data.NoteCategory" relationshipName="NoteCategoryMembers" direction="LinkIsTarget" />
    
</TypeDef>


现在查询的需求是查出所有typeof(NoteCategory)的对象,满足Parents.Count = 0的条件;
但是发现使用ObjectPath "Parents.Count=0"或"Parents[Count]=0"都无法进行查询,还抛出了异常;于是就到了Base4.Net的Forums上去问,结果得到的答案是:

Ask (Me) :

I defined a Class "NoteCategory" that contains a attribute "Parents"

the attribute is a Collection

how to query the object typeof(NoteCategory) that Parents.Count = 0  ?????

How to constuct the objectpath ??

 

thanks very much!!!!


Answer: (Alex James)

I don't support embedding a count filter on a relationship in objectpath yet... sorry.

Although what you are asking for should be added... I will have a think about how in the next few days.
The problem is that I collapse calls to object.Relationship so that you are querying the related object not the actual relationship table.

What needs to be done is to add additional HardCoded filters to the object.Relationship so that you can do the sort of things you are asking for....I will probably add support for this before Beta3.

While you can't do it with object path you can do it...

If you use an ObjectCommand and write the SQL yourself.
Execute the ObjectCommand so it returns a reader (ExecuteReader) and contruct an ObjectDataReader around the returned reader so you can read your NoteCategory objects.

It depends how you defined your RelationshipDef, if Parent is defined with LinkIsSource and a RelationshipName = Parent
This would return you all NoteCategories without parents:
SELECT * FROM [NOTECATEGORY] WHERE ID NOT IN (SELECT  DISTINCT(Source) FROM [Maxtiviti.Storage.Relationship] WHERE Name = 'Parent')

When I get a chance I will post you a snippet on how to use this code with a ObjectCommand...


呵呵,真希望alex快出beta3版啊,要不我用以下的解决方案,有很大的性能问题啊:

public ItemCollection GetAllRootCategory()
        
{
            ItemCollection result 
= new ItemCollection();
            ItemCollection ic 
= StorageContext.Find(typeof(NoteCategory),null);
            
foreach(NoteCategory nc in ic)
            
{
                
if(nc.Parents.Count == 0)
                    result.Add(nc);
            }

            
return result;
        }

虽然可以用alex提出的方法写Sql解决,但是那样做不是相当的不优雅的吗?
posted on 2005-03-29 08:16  数码幽灵  阅读(563)  评论(0编辑  收藏  举报