我有一个项目,我正在努力,需要更改'BaseSortedCollection'类以允许重复.该类当前实现IEnumerable,IDisposable,ICollection和ISerializable.'BaseSortedCollection'存储具有ItemID(Int64)的项目,该ID在访问集合时用作密钥.我需要在集合中同时存在两个相同的项目(相同的ItemID),并且能够被检索.
我们正在使用2.0框架.
有什么建议?
提前致谢!
注意:这与其他问题类似,但不完全相同
我已经实现了一个IBusinessCollection界面.它既来自两者ICollection<T>,又来自旧的非泛型ICollection.我更喜欢抛弃旧的破坏ICollection,但我正在使用带有CollectionView的WPF数据绑定,它希望我实现旧的非泛型IList:-(
无论如何,接口看起来像这样:
public interface IBusinessCollection<T> : ICollection<T>, ICollection
{ }
public interface ICollection<T>
{ int Count { get; } }
public interface ICollection
{ int Count { get; } }
Run Code Online (Sandbox Code Playgroud)
由于使用依赖注入,我IBusinessCollection<T>使用它们的接口传递类型的对象,而不是具体的类型,所以我有这样的东西:
internal class AnonymousCollection : IBusinessCollection<string>
{
public int Count { get { return 5; } }
}
public class Factory
{
public static IBusinessCollection<string> Get()
{ return new AnonymousCollection(); }
}
Run Code Online (Sandbox Code Playgroud)
当我尝试调用此代码时,出现错误,如下所示: …
我的具有一对多映射的域类通常采用以下形式(未经测试的代码):
public Customer Customer
{
// Public methods.
public Order AddOrder(Order order)
{
_orders.Add(order);
}
public Order GetOrder(long id)
{
return _orders.Where(x => x.Id).Single();
}
// etc.
// Private fields.
private ICollection<Order> _orders = new List<Order>();
}
Run Code Online (Sandbox Code Playgroud)
我在处理一对多关系时看到的EF4代码示例公开了一个公共ICollection.
有没有办法通过暴露它们来持久化和恢复我的收藏?如果没有,似乎我的域对象将被设计为满足ORM的要求,这似乎违背了努力的精神.公开ICollection(使用它的Add等方法)似乎并不特别干净,也不是我的默认方法.
更新
发现这篇文章表明它在5月份是不可能的.当然,微软的海报确实表示他们"正在强烈考虑实施"它(我希望如此)而且我们已经半年了,所以也许有一些进步?
我试图理解ICollection中syncroot的意义.为什么不直接锁定集合?
lock(myCollection)
{
//do stuff to myCollection
}
Run Code Online (Sandbox Code Playgroud)
VS
lock(myCollection.SyncRoot)
{
//do stuff to myCollection
}
Run Code Online (Sandbox Code Playgroud) 我在使用Clear()从实体框架中的集合中删除所有元素时遇到问题
考虑一下常用的博客和帖子示例.
public class Blog
{
public int Id {get; set;}
public string Name {get; set;}
public virtual ICollection<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
// foreign key to Blog:
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
public string Title { get; set; }
public string Text { get; set; }
}
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs …Run Code Online (Sandbox Code Playgroud) 只是想对语法 sygar进行简单的扩展:
public static bool IsNotEmpty(this ICollection obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
public static bool IsNotEmpty<T>(this ICollection<T> obj)
{
return ((obj != null)
&& (obj.Count > 0));
}
Run Code Online (Sandbox Code Playgroud)
当我使用某些集合时,它工作得很好,但是当我与其他集合一起工作时,我得到了
以下方法或属性之间的调用不明确:“PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.IList)”和“PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.Generic.ICollection)”
这个问题有规范的解决方案吗?
不,我不想在调用此方法之前执行强制转换;)
我的web.config中有配置文件提供程序
<profile defaultProvider="MyProvider">
<providers>
.......
<properties>
<add name="CustomField1" type="string" />
<add name="CustomField2" type="string" />
<add name="CustomField3" type="string" />
<add name="CustomField4" type="string" />
</properties>
</profile>
Run Code Online (Sandbox Code Playgroud)
如何获取包含所有可用属性的string []数组(CustomField1,CustomField2 ....)
编辑: 找到工作解决方案,但不确定它是否是最好和最简单的.
var allCustomProperties =
profile.GetType().GetProperties().Where(l => l.PropertyType.Name == "String" && l.CanWrite == true).Select(
l => l.Name).ToArray();
Run Code Online (Sandbox Code Playgroud) 我的类继承了一个接口,因此我需要在 Person 类中拥有 emailaddress 属性。
我的问题是获取财产和设置财产的最佳方式是什么
public class Contact : IPerson, ILocation
{
...
[MaxLength(256)]
public string EmailAddress {
get{
return this.Emails.First().ToString();
}
set{ ???? }
}
....
public virtual ICollection<Emails> Emails { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
本质上,我试图让班级允许不止一封电子邮件。
为了充分披露,我对此很陌生,我可能没有问正确的问题,但我已经搜索了一天半,但没有看到这样的东西(并不是说我认为这是不寻常的)并且可以使用的洞察力。
电子邮件类属性:
[Key]
public int Id { get; set; }
[MaxLength(256)]
public string EmailAddress { get; set; }
Run Code Online (Sandbox Code Playgroud) 我想.IsEmpty()为 ICollection 和 IReadonlyCollection 接口编写一个扩展方法(例如):
public static bool IsEmpty<T>(this IReadOnlyCollection<T> collection)
{
return collection == null || collection.Count == 0;
}
public static bool IsEmpty<T>(this ICollection<T> collection)
{
return collection == null || collection.Count == 0;
}
Run Code Online (Sandbox Code Playgroud)
但是当我将它与实现两个接口的类一起使用时,我显然得到了“模棱两可的调用”。我不想打字myList.IsEmpty<IReadOnlyCollection<myType>>(),我只想打字myList.IsEmpty()。
这可能吗?
在 EF Core 中定义一对多关系的最佳实践是什么?
是否建议一般使用列表,因为它们提供更多功能?然后是接口 IList 而不是 List,因为它没有定义实现?如果确实重要,我应该根据什么标准选择其中之一?
Microsoft 建议使用以下 https://docs.microsoft.com/en-us/ef/core/modeling/relationships
public class Blog
{
public int BlogId { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然而
https://www.learnentityframeworkcore.com/configuration/one-to-many-relationship-configuration 建议 ICollections
以下 2011 年的答案在 EF Core 中仍然有效吗?
icollection ×10
c# ×8
.net ×2
ambiguous ×1
asp.net ×1
code-first ×1
collections ×1
database ×1
duplicates ×1
generics ×1
list ×1
properties ×1
syncroot ×1
web-config ×1