IEnumerable<ReportFavorite> list = reportService.GetReportFavorites(userId);
ddlReportFavorite.Items.Add()
I don't know how to add the lists to the dropdown using Linq. Thanks.
You an use AddRange method:
var list = reportService.GetReportFavorites(userId);
ddlReportFavorite.Items.AddRange(list.ToArray());
Depending on the dropdown control you are using, either of these could work:
If it allows its Items to be set to an IEnumerabe<ReportFavourite>:
ddlReportFavorite.Items = reportService.GetReportFavorites(userId);
If Items implements the AddRange method:
ddlReportFavorite.Items.AddRange(reportService.GetReportFavorites(userId));
Or, if these fail
foreach(var reportFavourite in reportService.GetReportFavorites(userId))
ddlReportFavorite.Items.Add(reportFavourite);
Neither of these methods is really "using LINQ", because LINQ is not a good tool to do this. LINQ is meant to be side-effect free.
Edit:
Your comment suggests that you are using a System.Web.UI.WebControls.DropDownList. In this case, the Items collection only accepts instances ListItem, so you need to create these from your ReportFavourites. Try
foreach(var listItem in reportService.GetReportFavorites(userId)
.Select(r => new ListItem(r.Id, r.Name))
ddlReportFavorite.Items.Add(listItem);
Here, I assume the combo box should display ReportFavourite.Name and have a value of ReportFavourite.Id. Use your own properties, of course
Or if you've already checked the data's integrity in the method, you could just simply say:
ddlReportFavorite.Items.AddRange(reportService.GetReportFavorites(userId));
Previously I put as IEnumerable. Now I changed to IList. It is working fine now. Thanks to all.
int userId = workContext.CurrentUser.UserID;
var reportFavoriteList = reportService.GetReportFavorites(userId);
int count = reportFavoriteList.Count;
for (int i = 0; i < count; i++)
{
ddlReportFavorite.Items.Add(reportFavoriteList[i].FavoriteName);
}
Since ddlReprotFavorite is an UI control and itsItemsproperty represent a set of controls as well you can not add directly your business entities instead of use DataSource property which automatically create Items collection from the underlying business entities.
IEnumerable<ReportFavorite> list = reportService.GetReportFavorites(userId);
ddlReportFavorite.DataSource = list;
The IEnumerable<T> is extended by the method Union<T> which unions two IEnumerable<T>'s. This is the more pretty way, without casting it ToList().
var reportFavoriteList = reportService.GetReportFavorites(userId);
ddlReportFavorite.Items = ddlReportFavorite.Items.Union(reportFavoriteList);
Related
I have a list of a custom type called Holdings. I am trying to query the list based on one property of the Holdings object to return a new list of Holdings. The LINQ query below does work correctly but I would like to replace var unitHld with List unitHld but can't get the code to work.
var unitHld = from hld in _holdingList
where hld.FundCode == lookThroList[i].FundCode
select new Holding() { Currency = hld.Currency,
FundCode = lookThroList[i].FundCode,
IdSedol = hld.IdSedol,
Nominal = hld.Nominal * unitWgt,
Price = hld.Price };
This new list is then slightly altered before being added back to the original list (I know the logic sounds strange but please accept this is how it has to be done). However because unitHld is var the line below does not work.
_holdingList.Add(unitHld);
The following call only adds a single item (where the item must be the same type as the list's elements):
_holdingList.Add(unitHld);
But you want to add a range of items, so do it like this:
_holdingList.AddRange(unitHld);
where unitHld is IEnumerable<T> and T is the type of the list's elements.
(This answer assumes that holdingList is of type List<T>, and that T is in fact Holding for your example.)
See List.AddRange() for details.
C# is statically typed.
var is not a type, all it does is a shortcut for in your case typing IEnumerable<Holding>.
If you want the result to be List<Holding> then all you need to do is wrap your query in brackets and put .ToList() at the end.
However, to append this to another list, you don't need to do that. Simply call .AddRange on the other list.
Alternatively, you can use Concat
var bothLists = aList.Concat(anotherList);
I would like to replace var unitHld with List unitHld but can't get the code to work.
You need to call ToList() on the result of the query:
var unitHld = from hld in _holdingList
where hld.FundCode == lookThroList[i].FundCode
select new Holding() { Currency = hld.Currency,
FundCode = lookThroList[i].FundCode,
IdSedol = hld.IdSedol,
Nominal = hld.Nominal * unitWgt,
Price = hld.Price };
List<Holding> unitHldList = unitHld.ToList();
This new list is then slightly altered before being added back to the original list
Once the data is in unitHldList, you can alter it as needed.
the line below does not work. _holdingList.Add(unitHld);
When you add the content of a collection to a List<T>, use AddRange method instead of Add:
_holdingList.AddRange(unitHldList);
Try this:
_holdingList.AddRange(unitHld);
How do I remove items from a IEnumerable that match specific criteria?
RemoveAll() does not apply.
You can't; IEnumerable as an interface does not support removal.
If your IEnumerable instance is actually of a type that supports removal (such as List<T>) then you can cast to that type and use the Remove method.
Alternatively you can copy items to a different IEnumerable based on your criteria, or you can use a lazy-evaluated query (such as with Linq's .Where) to filter your IEnumerable on the fly. Neither of these will affect your original container, though.
This will produce a new collection rather than modifying the existing one however I think it is the idiomatic way to do it with LINQ.
var filtered = myCollection.Where(x => x.SomeProp != SomValue);
Another option would be to use Where to produce a new IEnumerable<T> with references to the objects you want removed then pass that to a Remove call on the original collection. Of course that would actually consume more resources.
You can't remove items from an IEnumerable<T>. You can remove items from an ICollection<T> or filter items from an IEnumerable<T>.
// filtering example; does not modify oldEnumerable itself
var filteredEnumerable = oldEnumerable.Where(...);
// removing example
var coll = (ICollection<MyClass>)oldEnumerable;
coll.Remove(item);
You don't remove items from an IEnumerable. It's not possible. It's just a sequence of items. You can remove items from some underlying source that generates the sequences, for example if the IEnumerable is based on a list you can remove items from that list.
The other option you have is to create a new sequence, based on this one, that never shows the given items. You can do that using Where, but it's important to realize this isn't removing items, but rather choosing to show items based on a certain condition.
As everyone has already stated, you can't remove from IEnumerable because that is not what the interface is describing. Consider the following example:
public IEnumerable<string> GetSomeStrings()
{
yield return "FirstString";
yield return "Another string";
}
Clearly, removing an element from this IEnumerable is not something you can reasonably do, instead you'd have to make a new enumeration without the ones you don't want.
The yield keywork provides other examples, for example, you can have infinite lists:
public IEnumberable<int> GetPowersOf2()
{
int value = 1;
while(true)
{
yield return value;
value = value * 2;
}
}
Items cannot be removed from an IEnumerable<T>. From the documentation:
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
You can cast it and use the List<T>.RemoveAll(Predicate<T> match) this is exactly what you need.
This is how i do,
IEnumerable<T> myVar=getSomeData(); // Assume mayVar holds some data
myVar=myVar.Where(d=>d.Id>10); // thats all, i want data with Id>10 only
How about trying Enumerable.Empty i.e.
T obj = new T();
IEnumerable<T> myVar = new T[]{obj} //Now myVar has an element
myVar = Enumerable.Empty<T>(); //Now myVar is empty
Is there a way to remove all items except first one from any type of collection (Control.Items, List ....) using LINQ only ?
No. LINQ is designed for querying collections (no side-effects), not for adding or removing items.
What you can do is write a query that takes the first element of the collection:
var result = source.Take(1);
Note that LINQ doesn't work with all types of collections; you need a LINQ provider to make LINQ work. For instance, source must implement IEnumerable<T> to use the extension methods of the Enumerable Class (LINQ-to-Objects).
How about something using reflection?
static void RemoveButFirst(object o){
Type t = o.GetType();
System.Reflection.MethodInfo rm = t.GetMethod("RemoveAt",
new Type[]{typeof(int)});
System.Reflection.PropertyInfo count = t.GetProperty("Count");
for (int n = (int)(count.GetValue(o,null)) ; n>1; n--)
rm.Invoke(o, new object[]{n-1});
}
This would work any time your collection exposed an int Count property and a RemoveAt(int) method, which I think those collections should.
And a more concise version, using dynamic, if you work with C# 4.0:
public static void RemoveBut(dynamic col, int k){
for (int n = col.Count; n>k; n--)
col.RemoveAt(n-1);
}
You can use .Take(1), but it returns a new collection, and leaves the original intact.
The idea of LINQ came from functional programming where everything is immutable, because of that, they didn't make it possible to modify the collections with LINQ.
Jon Skeet has a comment on the subject: LINQ equivalent of foreach for IEnumerable<T>
How about (in linq):
var result = list.Where(l => l != list.First());
But this would be better:
var result = list.Take(1);
List<string> collection = new List<string>();
collection.RemoveAll(p => p.StartsWith("something"));
listXpto.Where(x=>true /* here goes your query */)
.Select(x=>{listXpto.Remove(x); return null})
But I donĀ“t know the real utility of that.
Remember that the remove method is for ILists, not IQueryable in general.
i have this entity called BlogArticle which has a property called
public virtual ICollection<BlogComment> BlogComments { get; set; }
what i wanna do is access those properties of the blogcomments in my view but since it is in a ICollection i cannot itterate through it. (.count() does work.)
Any suggestions on this matter?
cheers.
You can enumerate the collection by using a foreach loop. If you need random access to the elements of the collection you can use the ToList() extension method. That will create a new list containing all the elements of the collection.
foreach (var blogComment in blogArticle.BlogComments) {
// Access sequentially from first to last.
}
or
var blogComments = blogArticle.BlogComments.ToList();
for (var i = 0; i < blogComments.Count; ++i) {
var blogComment = blogComments[i]; // Access by index - can be done in any order.
}
ICollection is an interface, so it depends on how you are initializing this object i.e.
ICollection<BlogComment> BlogComments = new List<BlogComment>();
Would allow you to do...
BlogComments.Count;
For future reference being that the question is a bit old... I'm surprised that no one mentioned this before, but if you do not want to do the cast in either the view model or the view itself you could do:
#for(int i = 0; i < model.BlogComments.Count; i++)
{
#Html.DisplayFor(model => model.BlogComments.ElementAt(i));
}
EDIT: I will add that this is only a useful strategy for displaying data (edited the example to show this), not a useful strategy inside of a form where you want to have the values posted back to your action method when used in HTML helpers like EditorFor. The name property of input is not formed in a way that will allow the model binder to bind these values back to the collection. You would either have to write the name out by hand (not robust) have some sort of intermediary collection type with the index operator like IList that you keep in sync with the ICollection.
I am using C# and targeting the .NET Framework 3.5. I'm looking for a small, succinct and efficient piece of code to copy all of the items in a ListBox to a List<String> (Generic List).
At the moment I have something similar to the below code:
List<String> myOtherList = new List<String>();
// Populate our colCriteria with the selected columns.
foreach (String strCol in lbMyListBox.Items)
{
myOtherList.Add(strCol);
}
Which works, of course, but I can't help but get the feeling that there must be a better way of doing this with some of the newer language features. I was thinking of something like the List.ConvertAll method but this only applies to Generic Lists and not ListBox.ObjectCollection collections.
A bit of LINQ should do it:-
var myOtherList = lbMyListBox.Items.Cast<String>().ToList();
Of course you can modify the Type parameter of the Cast to whatever type you have stored in the Items property.
The following will do it (using Linq):
List<string> list = lbMyListBox.Items.OfType<string>().ToList();
The OfType call will ensure that only items in the listbox's items that are strings are used.
Using Cast, if any of the the items are not strings, you will get an exception.
How about this:
List<string> myOtherList = (from l in lbMyListBox.Items.Cast<ListItem>() select l.Value).ToList();
What about:
myOtherList.AddRange(lbMyListBox.Items);
EDIT based on comments and DavidGouge's answer:
myOtherList.AddRange(lbMyListBox.Items.Select(item => ((ListItem)item).Value));
You don't need more. You get List of all values from Listbox
private static List<string> GetAllElements(ListBox chkList)
{
return chkList.Items.Cast<ListItem>().Select(x => x.Value).ToList<string>();
}