I am using the COM interface to TFS. (TeamFoundationServer.ExtendedClient v14.95.3). I am trying to use LINQ to iterate over the various collections. For example, this function works great:
public static IEnumerable<string> GetTitles(WorkItemCollection workItemList)
{
return from WorkItem item in workItemList select item.Fields["Title"].Value.ToString();
}
However, when I try to change to use the method syntax it fails:
public static IEnumerable<string> GetTitles2(WorkItemCollection workItemList)
{
return workItemList.Select(item => item.Fields["Title"].Value.ToString());
}
... gives me error "'WorkItemCollection' does not contain a definition for 'Select'..."
I have using System.Linq; in my file. And I am referencing System.Core.dll. The WorkItemCollection does implement IEnumerable. So why doesn't this work?
WorkItemCollection does only implement IEnumerable, but not IEnumerable<WorkItem>. The LINQ extensions are declared only for IEnumerable<T>, not for the non-generic IEnumerable.
What you can do is use OfType<T>():
public static IEnumerable<string> GetTitles2(WorkItemCollection workItemList)
{
return workItemList.OfType<WorkItem>()
.Select(item => item.Fields["Title"].Value.ToString());
}
Instead of OfType<T> you can also use Cast<T>. But if there is something other than a WorkItem in the sequence (which is probably never the case in this scenario), Cast<T> would throw an InvalidCastException while OfType<T> would ignore that element.
WorkItemCollection implements IEnumerable, not IEnumerable<T>. It's the latter, generic, interface that is the foundation of LINQ.
You can convert from one to the other using the Cast<T> extension method, however:
workItemList.Cast<WorkItem>.Select(item => ...
Related
I have an API which returns System.Collections.Generic.IEnumerable<t>, if I import(using) System.Linq. I am able to do Count() / ToList() on the returned object.
What would be the relation between those two(System.Collections.Generic, System.Linq), packages ?
Thanks
Pavan
IEnumerable<> is that relationship. Those methods (Count(), ToList()) are extension methods on IEnumerable<>
This means that anything that implements IEnumerable (IList, ICollection, Arrays) they all will have those methods. To use them you need to "import" those methods with Using System.Linq; statement
A simple extension method on String. can be
public static class StringExtensions
{
public static string CountWords(this string input )
{
return imnput.Split(new char[]{' '}).Length;
}
}
There is a static class Enumerable in the System.Linq namespace which provides the most of extension methods you use with IEnumerable<T> types.
I would like to pass an IQueryable and an array of ids to a method which filters the IQueryable based on those ids.
As the ids can be either long's or int's it should be solved generically.
I came up with the following:
public static IEnumerable<T> GetModified<TId, T>(IQueryable<T> objects, TId[] ids) where T : class
{
return objects.Where(j => ids.Contains((TId)j.GetType().GetProperty("Id").GetValue(j)));
}
Unfortunately I'm getting the exception:
LINQ to Entities does not recognize the method 'System.Object GetValue(System.Object)' method, and this method cannot be translated into a store expression.
The exception is normal, as getting properties through reflection is something that clearly cannot be translated to SQL.
One thing I would try is to create a generic interface that exposes an Id property of a given type:
public interface HasId<T> {
T Id { get; set; }
}
Now you could declare your entity as implementing HasId<int>, for example, if the Id was of type int.
The next step is to modify your method like so:
public static IEnumerable<T> GetModified<TId, T>
(IQueryable<T> objects, TId[] ids) where T : class, HasId<TId>
{
return objects.Where(j => ids.Contains(j.Id));
}
Note the added generic restriction: where T : class, HasId<TId>. This enables you to write the simplified j.Id, which returns a TId value, instead of resorting to reflection.
Please note that I haven't run or tested this code; it's just an idea that I got when I saw your problem and I hope it helps.
Update:
Here's another possible solution that doesn't require that you declare interfaces or change your classes in any way:
public static IEnumerable<T> GetModified<TId, T>
(IQueryable<T> objects, TId[] ids, Expression<Func<T, TId>> idSelector)
where T : class
{
return objects.Where(j => ids.Contains(idSelector(j)));
}
What I've done here is add the Expression<Func<T, TId>> idSelector parameter, an expression that can return the Id of a given instance of T.
You would call the method like that:
var modified = GetModified(dbObjects, yourIdArray, entity => entity.Id);
(only the third parameter being new; keep the others as you have them now).
Again, I haven't tested if this works or even compiles, as I don't have a computer with VS here :(.
Entity Framework doesn't support some of the .NET methods such as GetValue() since it does not translate to SQL (which is the code actually executed to the IQueryable. Try calling ToList to get the CLR object before doing reflection:
public static IEnumerable<T> GetModified<TId, T>(IQueryable<T> objects, TId[] ids) where T : class
{
return objects.ToList().Where(j => ids.Contains((TId)j.GetType().GetProperty("Id").GetValue(j)));
}
I have a class in my code that is already deriving from IEnumerable.
I was wondering if there is a way that I can use a "Contains" method on its instnaces to look for a something in that list?
Do you really implement the non-generic IEnumerable, or the generic IEnumerable<T>? If you can possibly implement the generic one, your life will become a lot simpler - as then you can use LINQ to Objects, which does indeed have a Contains extension method.
Otherwise, you could potentially convert from the non-generic to generic using Cast or OfType, e.g.
bool found = nonGeneric.Cast<TargetType>().Contains(targetItem);
It would be nicer if you just implemented the generic interface to start with though :)
No, there's no such method in the IEnumerable<T> interface. There's an extension method though that you could use.
using System.Linq;
and then:
IEnumerable<string> foos = new[] { "foo", "bar", "baz" };
bool IsThereABar = foos.Contains("bar");
public static bool Contains<T>(this IEnumerable source, T value)
{
foreach (var i in source)
{
if (Equals(i, value))
return true;
}
return false;
}
If you want, you can add custom comparer as parameter ti extension method Contains
If I try this, it will work:
var query = myContextObject.Users.Where(u=>u.Name == "John");
query.ToList();
I'm able to call ToList and a lot of other extension methods.
But if I try this:
public List ConvertQueryToList(IQueryable query)
{
return query.ToList();
}
ToList won't be accessible, I'm guessing this is because ToList is an extension method, but then how is that ToList is attached in the first example?
Is it possible to access ToList in the second case?
You need to write it as:
public List<T> ConvertQueryToList<T>(IQueryable<T> query)
{
return query.ToList();
}
This will cause the IQueryable<T> to return the appropriate List<T>, since the Enumerable.ToList() method requires an IEnumerable<T> as input (which also works with IQueryable<T>, as IQueryable<T> inherits IEnumerable<T>).
That being said, there is really no reason to use it this way. You can always just call ToList() directly if you need to create a List<T> - abstracting inside of a second layer just confuses the API further.
If you're trying to convert a non-generic IQueryable interface, you would need to do something like:
public List<T> ConvertQueryToList<T>(IQueryable query)
{
return query.Cast<T>.ToList();
}
This would then require calling like:
var results = ConvertQueryToList<SomeType>(queryable);
Alternatively, if you want to leave this non-generic (which I wouldn't recommend), then you could use:
public ArrayList ConvertQueryToList(IQueryable query)
{
ArrayList results = new ArrayList();
results.AddRange(query.Cast<object>().ToList());
return results;
}
The first of your examples returns an IQueryable<T>, whereas in the second you're using IQueryable (without the Generic Type parameter).
You can check out the two completely different interfaces here and here.
Here is an extension method for this:
public static class ListHelper
{
public static IList ToList(this IQueryable query)
{
var genericToList = typeof(Enumerable).GetMethod("ToList")
.MakeGenericMethod(new Type[] { query.ElementType });
return (IList)genericToList.Invoke(null, new[] { query });
}
}
Here is a generic extension method for the case you are using IQueryable<>.
Of course it is not absolutely safe because the type could be wrong and the cast could fail. So please be careful if you use that method.
using System.Collections.Generic;
namespace System.Linq
{
public static class Extensions
{
public static List<T> ToList<T>(this IQueryable queriable)
{
return ((IQueryable<T>)queriable).ToList();
}
}
}
I have a HQL query that can generate either an IList of results, or an IEnumerable of results.
However, I want it to return an array of the Entity that I'm selecting, what would be the best way of accomplishing that? I can either enumerate through it and build the array, or use CopyTo() a defined array.
Is there any better way? I went with the CopyTo-approach.
Which version of .NET are you using? If it's .NET 3.5, I'd just call ToArray() and be done with it.
If you only have a non-generic IEnumerable, do something like this:
IEnumerable query = ...;
MyEntityType[] array = query.Cast<MyEntityType>().ToArray();
If you don't know the type within that method but the method's callers do know it, make the method generic and try this:
public static void T[] PerformQuery<T>()
{
IEnumerable query = ...;
T[] array = query.Cast<T>().ToArray();
return array;
}
Put the following in your .cs file:
using System.Linq;
You will then be able to use the following extension method from System.Linq.Enumerable:
public static TSource[] ToArray<TSource>(this System.Collections.Generic.IEnumerable<TSource> source)
I.e.
IEnumerable<object> query = ...;
object[] bob = query.ToArray();
I feel like reinventing the wheel...
public static T[] ConvertToArray<T>(this IEnumerable<T> enumerable)
{
if (enumerable == null)
throw new ArgumentNullException("enumerable");
return enumerable as T[] ?? enumerable.ToArray();
}
In case you don't have Linq, I solved it the following way:
private T[] GetArray<T>(IList<T> iList) where T: new()
{
var result = new T[iList.Count];
iList.CopyTo(result, 0);
return result;
}
Hope it helps