Using reflector I have noticed that System.Linq.Enumerable.Count method has a condition in it to optimize it for the case when the IEnumerable<T> passed is in fact an ICollection<T>. If the cast succeeds the Count method does not need to iterate over every element, but can call the Count method of ICollection.
Based on this I was starti...
I've gotta be missing something simple here.
Take the following code:
public IEnumerable<int> getInt(){
for(int i = 0; i < 10; i++){
yield return i;
}
}
I can call this with:
foreach (int j in obj.getInt()){
//do something with j
}
How can I use the getInt method without the foreach loop:
IEnumerable<int> iter = obj.getI...
Is there any better/nicer way to do the following?
An object that can either be used as a normal object and access it's properties and/or as an IEnumerable collection of the object. See code below:
Sample:
static void Main(string[] args)
{
Item i = new Item() {Name="Erik"};
i.Add(new Item() {Name="Fred"});
i.Add(new Item...
Here's an example of the architecture approach I favorited as for now:
public abstract class CollectionComparer {
public virtual SetEqual(IEnumerable enum1, IEnumerable enum2) {
if(enum1== null && enum2== null)
return true;
if(enum1== null && !(enum2== null))
return false;
if(!(enum1...
I have generated the following code with Entity Framework 4 for a navigation property:
<XmlIgnoreAttribute()>
<SoapIgnoreAttribute()>
<DataMemberAttribute()>
<EdmRelationshipNavigationPropertyAttribute("Diagnose", "Request_Comments", "Comment")>
Public Property Comments() As EntityCollection(Of Comment)
Get
Return CType(Me,I...
In Entity framework, would the statement
MyDataContext.Products.OrderByDescending( (p) => p.Id ).GroupBy<Product,int>( (p) => p.ProductId ).Select( (g) => g.FirstOrDefault()).Where( (p) => p.Name.Equals("Something") );
result in another database query than
MyDataContext.Products.Where( (p) => p.Name.Equals("Something") ).OrderByDesce...
When the property have some collection type like
public IList<MyItemClass> MyList{ get; }
IMHO, it's better to return empty collection instead of null value.
There are many ways to implement such functionality.
public IList<MyItemClass> MyList{ get; private set; }
public MyClass(){
MyList = new List<MyItemClass>();
}
This way ...
I have a linq query that is querying over IEnumberable. When I have a matching element for my where clause I would like to know the position of the element in the IEnumberable.
var result = from e in elements
where (string) e.Attribute("class") == "something"
select e.Position();
The e.Position() of course do...
Is there a better way getting the first element of IEnumerable type of this:
foreach (Image image in imgList)
{
picture.Width = (short)image.Columns;
picture.Height = (short)image.Rows;
break;
}
This is the exact declaration of the type:
public class ImageList : IEnumerable, IDisposable
...
I cannot get to the bottom of this error because it happens only in one instance, and I can't find any code that can be the cause of the error.
I have a 3.5 web service that I'm calling from a multi-threaded, CAB client. I have a bunch of unit tests against the web service (from both 3.5 and 2.0 code), and it works fine. However, in th...
I'd like to create an IList<Child> that maintains its Child objects in a default/implicit sort order at all times (i.e. regardless of additions/removals to the underlying list).
What I'm specifically trying to avoid is the need for all consumers of said IList<Child> to explicitly invoke IEnumerable<T>.OrderBy() every time they want to...
I've been trying to learn more about using Lamba expression trees and so I created a simple example. Here is the code, this works in LINQPad if pasted in as a C# program.
void Main()
{
IEnumerable<User> list = GetUsers().Where(NameContains("a"));
list.Dump("Users");
}
// Methods
public IEnumerable<User> GetUsers()
{
yield r...
I have the following code :
XDocument xResponse = XDocument.Parse(strXMLResponse);
var vMyData = from xmyInfo in xResponse.Descendants("Result").Elements("item")
select new myProporties
{
strmyInfo1 = ((string)xmyInfo .Element("el1")).Trim(),
strmyInfo2 = ((string)xmyInfo .Element("el2")).Trim(),
strmyInfo3 = (strin...
Hi
I have a need to move an item in an IEnumerable<> up, that is move one item above another. What is the simplest way to do this?
A similar question was asked here but I don't have a generic list only an IEnumerable<>: http://stackoverflow.com/questions/450233/generic-list-moving-an-item-within-the-list
...
I'm confused as to the difference. Being fairly new to .Net, I know I can query IEnumerables using the Linq extensions. So what is this IQueryable and how does it differ?
...
Here's my code:
Event thisEvent = (from i in list
where (i.eventID == eventID)
select i).FirstOrDefault();
if (thisEvent != null)
{
thisEvent.eventResolved = resolved;
thisEvent.eventSequence.Add(item);
}
"list" is a collectio...
Hi,
In the .net framework, there's a generic IEnumerable<T> interface which inherits from the not-generic IEnumerable, and they both have a GetEnumerator() method. The only differents between these two GetEnumerator() is the return type.
Now I have a similar design, but when I compile the code, the compiler said:
MyInterface<T>.GetIte...
I have a list of objects and I'd like to update a particular member variable within one of the objects. I understand LINQ is designed for query and not meant to update lists of immutable data. What would be the best way to accomplish this? I do not need to use LINQ for the solution if it is not most efficient.
Would creating an Update ...
I've got two List<Name>s:
public class Name
{
public string NameText {get;set;}
public Gender Gender { get; set; }
}
public class Gender
{
public decimal MaleFrequency { get; set; }
public decimal MaleCumulativeFrequency { get; set; }
public decimal Fem...
Why does an event handler never get called if it's added within a loop on an ienumerable?
For instance:
IEnumerable<MyType> list = someCollection.Select(i => new MyType(i));
foreach (var item in list)
item.PropertyChanged += item_PropertyChanged; <-- this never gets called
Bu if list is assigned like
list = someCollection.Select(i ...