标签: icollection

需要在SortedCollection中允许重复(C#,2.0)

我有一个项目,我正在努力,需要更改'BaseSortedCollection'类以允许重复.该类当前实现IEnumerable,IDisposable,ICollection和ISerializable.'BaseSortedCollection'存储具有ItemID(Int64)的项目,该ID在访问集合时用作密钥.我需要在集合中同时存在两个相同的项目(相同的ItemID),并且能够被检索.

我们正在使用2.0框架.

有什么建议?

提前致谢!

c# collections duplicates icollection

6
推荐指数
2
解决办法
780
查看次数

ICollection与ICollection <T> - ICollection <T> .Count与ICollection.Count之间的歧义

注意:这与其他问题类似,但不完全相同

我已经实现了一个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)

当我尝试调用此代码时,出现错误,如下所示: …

c# generics icollection

6
推荐指数
1
解决办法
2163
查看次数

当我在EF4代码中有一对多映射时,我可以隐藏我的ICollection <T>字段吗?

我的具有一对多映射的域类通常采用以下形式(未经测试的代码):

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月份是不可能的.当然,微软的海报确实表示他们"正在强烈考虑实施"它(我希望如此)而且我们已经半年了,所以也许有一些进步?

entity-framework icollection code-first entity-framework-4

6
推荐指数
1
解决办法
1073
查看次数

为什么要锁定Collection.SyncRoot而不是只锁定集合?

我试图理解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)

.net icollection syncroot

6
推荐指数
1
解决办法
5060
查看次数

如何清除()实体框架ICollection中的所有元素?

我在使用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)

c# entity-framework icollection

6
推荐指数
2
解决办法
8336
查看次数

ICollection / ICollection&lt;T&gt; 歧义问题

只是想对语法 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)”

这个问题有规范的解决方案吗?

不,我不想在调用此方法之前执行强制转换;)

c# extension-methods icollection ambiguous

5
推荐指数
1
解决办法
2233
查看次数

C#从SettingsPropertyCollection获取属性

我的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)

.net c# asp.net web-config icollection

5
推荐指数
1
解决办法
1784
查看次数

在 C# 中设置 ICollection 属性的值

我的类继承了一个接口,因此我需要在 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)

c# properties icollection

5
推荐指数
1
解决办法
6015
查看次数

ICollection 和 IReadOnlyCollection 的扩展方法

我想.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()

这可能吗?

c# icollection

5
推荐指数
1
解决办法
1447
查看次数

对 EF Core 中的 1 到 n 关系使用 IList、List、ICollection

在 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)

然而

http://www.entityframeworktutorial.net/efcore/configure-one-to-many-relationship-using-fluent-api-in-ef-core.aspx

https://www.learnentityframeworkcore.com/configuration/one-to-many-relationship-configuration 建议 ICollections

以下 2011 年的答案在 EF Core 中仍然有效吗?

/sf/answers/535918211/

c# database entity-framework list icollection

5
推荐指数
1
解决办法
3777
查看次数