EntitySet与EntityList到底有什么本质区别?各自最恰当的使用场合是?

xhtmldivcss 2011-05-31 04:38:41
public class EntityList<T> : ObservableCollection<T> where T : global::System.ServiceModel.DomainServices.Client.Entity

public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged

public sealed class EntitySet<TEntity> : EntitySet, IEnumerable<TEntity>, IEnumerable, ICollectionViewFactory where TEntity : global::System.ServiceModel.DomainServices.Client.Entity

public abstract class EntitySet : IEnumerable, INotifyCollectionChanged, IRevertibleChangeTracking, IChangeTracking, INotifyPropertyChanged


从以上可以看出:
1、EntitySet和EntityList都实现了我们经常关注的INotifyPropertyChanged和INotifyCollectionChanged.

2、EntityList是个ObservableCollection.

3、EntityList有个Source属性。

4、从EntityList的构造函数可以看出它backed by an EntitySet.
public EntityList(EntitySet<T> entitySet);
//
// Summary:
// Initializes a new instance of the Microsoft.Windows.Data.DomainServices.EntityList<T>
// with the specified source
//
// Parameters:
// entitySet:
// The System.ServiceModel.DomainServices.Client.EntitySet<TEntity> that backs
// this list. All items added or removed from this list will also be added or
// removed from the backing Microsoft.Windows.Data.DomainServices.EntityList<T>.EntitySet.
//
// source:
// The source collection used to populate this list
public EntityList(EntitySet<T> entitySet, IEnumerable<T> source);


那么除了经常作为DomainCollectionView(loader, source);中的source部分之外,EntityList<T>到底有什么主要作用?


...全文
704 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xh308 2011-07-25
  • 打赏
  • 举报
回复
山之魂2 2011-07-25
  • 打赏
  • 举报
回复
顶楼主,本人末有研究得这么细
xiongyu2006 2011-07-25
  • 打赏
  • 举报
回复
我靠,这不是技术帖吗?
Louis-Lv 2011-07-25
  • 打赏
  • 举报
回复
来接分了
zhu6789888 2011-07-25
  • 打赏
  • 举报
回复
haitang59 2011-07-25
  • 打赏
  • 举报
回复
小基 2011-07-25
  • 打赏
  • 举报
回复
net_friends 2011-07-25
  • 打赏
  • 举报
回复
mibbadman 2011-07-25
  • 打赏
  • 举报
回复
2011-07-25
  • 打赏
  • 举报
回复
囧,这么没人气啊 都没人回复。
xhtmldivcss 2011-07-25
  • 打赏
  • 举报
回复
谁回一个,好让我回收点可用分……
xhtmldivcss 2011-07-24
  • 打赏
  • 举报
回复
说来也非常简单。每次DomainContext.Load不同数据的时候,EntitySet<T>会不停地累加新数据。比如一开始Load前2个对象,再Load2个不同数据时,EntitySet<T>里会有4个记录,而EntityList<T>只会显示最后Load的那些数据。


//ViewModel
public class ProductsViewModel : NotificationObject
{
private int id = 1;
private NorthwindDomainContext _Context = new NorthwindDomainContext();
public ProductsViewModel()
{
if (!DesignerProperties.IsInDesignTool)
{
IsBusy = true;
EntityQuery<Product> query = _Context.GetProductsQuery();
query = from p in query
where p.ProductID == id || p.ProductID == (id + 1)
select p;

_Context.Load(query, LoadBehavior.MergeIntoCurrent, OnLoadProductsCompleted, null);
}
}
public ICommand LoadMoreProducts
{
get
{
return new DelegateCommand(OnLoadMoreProducts);
}
}
// 左侧DataGrid绑定EntitySet<Product>
public EntitySet<Product> Products
{
get { return _Context.Products; }
}
private EntityList<Product> _productsList;
// 右侧DataGrid绑定EntityList<Product>
public EntityList<Product> ProductsList
{
get
{
if (_productsList == null)
_productsList = new EntityList<Product>(Products);

return _productsList;
}
}
void OnLoadMoreProducts()
{
IsBusy = true;
EntityQuery<Product> query = _Context.GetProductsQuery();
query = from p in query
where p.ProductID == id || p.ProductID == (id + 1)
select p;

_Context.Load(query, LoadBehavior.MergeIntoCurrent, OnLoadProductsCompleted, null);
}
private bool _isBusy = false;
public bool IsBusy
{
get
{
return _isBusy;
}
set
{
if (_isBusy != value)
{
_isBusy = value;
RaisePropertyChanged(() => this.IsBusy);
}
}
}

void OnLoadProductsCompleted(LoadOperation<Product> lo)
{
IsBusy = false;

if (lo.HasError)
{
MessageBox.Show(lo.Error.Message);
lo.MarkErrorAsHandled();
}
else if (!lo.IsCanceled)
{
id++;
ProductsList.Source = lo.Entities;
}
}
}

开始时:


当点击Load More Products按钮,读取的是ProductID为2、3时界面:
xhtmldivcss 2011-07-23
  • 打赏
  • 举报
回复
暂时不管DomainCollectionView了。

用简单的代码做比较。
默认读取ProductID为1和2的二条记录,点击界面按钮执行LoadMoreProducts命令时(这回读取ProductID为1和3的记录),控件绑定EntityList<T>和EntitySet<T>的结果不一样。绑定EntitySet<T>时,显示1、2、3三条记录。绑定EntityList<T>时,只显示1、3二条记录。



public class ProductsViewModel : NotificationObject
{
private NorthwindDomainContext _Context = new NorthwindDomainContext();

public ICommand LoadMoreProducts
{
get
{
return new DelegateCommand(OnLoadMoreProducts);
}
}
public ICommand SaveChanges
{
get
{
return new DelegateCommand(OnSave);
}
}
void OnSave()
{
_Context.SubmitChanges();
}


void OnLoadMoreProducts()
{
//_Context.Products.Clear();

EntityQuery<Product> query = _Context.GetProductsQuery();
//query = query.Where(p => p.ProductID == 3 || p.ProductID == 4);
query = from p in query
where p.ProductID == 1 || p.ProductID == 3
select p;


_Context.Load(query, LoadBehavior.MergeIntoCurrent, OnLoadProductsCompleted, null);
}
//当界面控件绑定EntityList<Products>时,执行LoadMoreProducts命令的结果是
//ProductID为1、2、3的三个记录。
public EntitySet<Product> Products
{
get
{
return _Context.Products;
}
}
//当界面控件绑定EntityList<Product>时,执行LoadMoreProducts命令的结果是
//ProductID为1、3的二条记录。
public EntityList<Product> _productList;
public EntityList<Product> ProductList
{
get
{
if(_productList == null)
_productList = new EntityList<Product>(Products);

return _productList;
}
}

public ProductsViewModel()
{
if (!DesignerProperties.IsInDesignTool)
{
EntityQuery<Product> query = _Context.GetProductsQuery();
query = query.Where(p => p.ProductID == 1 || p.ProductID == 2);
_Context.Load(query, LoadBehavior.RefreshCurrent, OnLoadProductsCompleted, null);
}

}
void OnLoadProductsCompleted(LoadOperation<Product> lo)
{
if (lo.HasError)
{
MessageBox.Show(lo.Error.Message);
lo.MarkErrorAsHandled();
}
else if (!lo.IsCanceled)
{
ProductList.Source = lo.Entities;
}

}

}


我觉得应该有个很关键的地方没想明白。哪位能指点一下,谢谢。
Join操作 适用场景:在我们表关系中有一对一关系,一对多关系,多对多关系等。对各个表之间的关系,就用这些实现对多个表的操作。 说明:在Join操作中,分别为Join(Join查询), SelectMany(Select一对多选择)和GroupJoin(分组Join查询)。 该扩展方法对两个序列中键匹配的元素进行inner join操作 SelectMany 说明:我们在写查询语句时,如果被翻译成SelectMany需要满足2个条件。1:查询语句中没有join和into,2:必须出现EntitySet。在我们表关系中有一对一关系,一对多关系,多对多关系等,下面分别介绍一下。 1.一对多关系(1 to Many): var q = from c in db.Customers from o in c.Orders where c.City == "London" select o; 语句描述:Customers与Orders是一对多关系。即Orders在Customers类中以EntitySet形式出现。所以第二个 from是从c.Orders而不是db.Orders里进行筛选。这个例子在From子句中使用外键导航选择伦敦客户的所有订单。 var q = from p in db.Products where p.Supplier.Country == "USA" && p.UnitsInStock == 0 select p; 语句描述:这一句使用了p.Supplier.Country条件,间接关联了Supplier表。这个例子在Where子句中使用外键导航筛选其供应商在美国且缺货的产品。生成SQL语句为: SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID],[t0].[QuantityPerUnit],[t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder],[t0].[ReorderLevel], [t0].[Discontinued] FROM [dbo].[Products] AS [t0] LEFT OUTER JOIN [dbo].[Suppliers] AS [t1] ON [t1].[SupplierID] = [t0].[SupplierID] WHERE ([t1].[Country] = @p0) AND ([t0].[UnitsInStock] = @p1) -- @p0: Input NVarChar (Size = 3; Prec = 0; Scale = 0) [USA] -- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [0] 2.多对多关系(Many to Many): var q = from e in db.Employees from et in e.EmployeeTerritories where e.City == "Seattle" select new { e.FirstName, e.LastName, et.Territory.TerritoryDescription }; 说明:多对多关系一般会涉及三个表(如果有一个表是自关联的,那有可能只有2个表)。这一句语句涉及Employees, EmployeeTerritories, Territories三个表。它们的关系是1:M:1。Employees和Territories没有很明确的关系。 LINQ to SQL语句之Join和Order By部分代码 语句描述:这个例子在From子句中使用外键导航筛选在西雅图的雇员,同时列出其所在地区。这条生成SQL语句为: SELECT [t0].[FirstName], [t0].[LastName], [t2].[TerritoryDescription] FROM [dbo].[Employees] AS [t0] CROSS JOIN [dbo].[EmployeeTerritories] AS [t1] INNER JOIN [dbo].[Territories] AS [t2] ON [t2].[TerritoryID] = [t1].[TerritoryID] WHERE ([t0].[City] = @p0) AND ([t1].[EmployeeID] = [t0].[EmployeeID]) -- @p0: Input NVarChar (Siz

8,734

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧