icollection

Extension methods for both ICollection and IReadOnlyCollection

ⅰ亾dé卋堺 提交于 2019-12-12 12:29:49
问题 I want to write an extension method (e.g. .IsEmpty() ) for both ICollection and IReadonlyCollection interfaces: 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; } But when I use it with classes implemeting both interfaces, I obviously get the ‘ambiguous invocation’. I don't want to type myList.IsEmpty

What is the real advantage of returning ICollection<T> instead of a List<T>? [duplicate]

北城以北 提交于 2019-12-12 08:20:04
问题 This question already has answers here : Closed 10 years ago . I've read a couple of blog post mentioning that for public APIs we should always return ICollection (or IEnumerable) instead of List. What is the real advantage of returning ICollection instead of a List? Thanks! Duplicate: What is the difference between List (of T) and Collection(of T)? 回答1: An enumerator only returns one entity at a time as you iterate over it. This is because it uses a yield return . A collection, on the other

Can I hide my ICollection<T> fields when I have a one-to-many mapping in EF4 code-only?

一世执手 提交于 2019-12-09 12:28:30
问题 My domain classes that have one-to-many mappings generally take the following form (untested code): 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>(); } The EF4 code-only samples I've seen expose a public ICollection when dealing with one-to-many relationships. Is there a way to

Add to an ICollection

a 夏天 提交于 2019-12-09 08:49:18
问题 I am currently writing a C# project and I need to do unit testing for the project. For one of the methods that I need to unit test I make use of an ICollection which is normally populated from the selected items of a list box. When I create a unit test for the method it creates the line ICollection icollection = null; //Initialise to an appropriate value How can I create an instance of this ICollection and an item to the collection? 回答1: ICollection is an interface, you can't instantiate it

Why ICollection index does not work when instantiated?

瘦欲@ 提交于 2019-12-09 08:21:39
问题 When we declare a parameter as ICollection and instantiated the object as List, why we can't retrive the indexes? i.e. ICollection<ProductDTO> Products = new List<ProductDTO>(); Products.Add(new ProductDTO(1,"Pen")); Products.Add(new ProductDTO(2,"Notebook")); Then, this will not work: ProductDTO product = (ProductDTO)Products[0]; What is the bit I am missing? [Yes, we can use List as declaration an it can work, but I don't want to declare as list, like: List<ProductDTO> Products = new List

Does AsQueryable() on ICollection really makes lazy execution?

本小妞迷上赌 提交于 2019-12-08 19:21:07
问题 I am using Entity Framework CodeFirst where I have used Parent Child relations using ICollection as public class Person { public string UserName { get;set} public ICollection<Blog> Blogs { get; set;} } public class Blog { public int id { get; set; } public string Subject { get; set; } public string Body { get; set; } } Ok, so far everything is working ok, but my concern is, whenever I want to get the Blogs of a person, I get it as var thePerson = _context.Persons.Where(x => x.UserName = 'xxx'

How to Clear() all elements from Entity Framework ICollection?

荒凉一梦 提交于 2019-12-07 03:01:11
问题 I have problems removing all elements from a collection in entity framework using Clear() Consider the often used example with Blogs and Posts. 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

How to Clear() all elements from Entity Framework ICollection?

守給你的承諾、 提交于 2019-12-05 07:08:35
I have problems removing all elements from a collection in entity framework using Clear() Consider the often used example with Blogs and Posts. 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 {get; set;} public DbSet<Post> Posts {get; set;}

ICollection / ICollection<T> ambiguity problem

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 00:50:37
问题 Just want to make simple extension for syntactic 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)); } It works perfectly when I work with some collections, but when working with others I get The call is ambiguous between the following methods or properties: 'PowerOn.ExtensionsBasic.IsNotEmpty(System.Collections.IList)' and 'PowerOn

Must IList be finite?

允我心安 提交于 2019-12-04 01:01:12
Must .NET's IList be finite? Suppose I write a class FibonacciList implementing IList<BigInteger> The property Item[n] returns the nth Fibonacci number. The property IsReadOnly returns true. The methods IndexOf and Contains we can implement easily enough because the Fibonacci sequence is increasing - to test if the number m is Fibonacci, we need only to compute the finite sequence of Fibonacci numbers up to m. The method GetEnumerator() doing the right thing We've now implemented all the methods expected of read-only ILists except Count(). Is this cool, or an abuse of IList? Fibonacci numbers