How to Convert Linq Results to Hashset or Hashedset

How to convert linq results to HashSet or HashedSet

I don't think there's anything built in which does this... but it's really easy to write an extension method:

public static class Extensions
{
public static HashSet<T> ToHashSet<T>(
this IEnumerable<T> source,
IEqualityComparer<T> comparer = null)
{
return new HashSet<T>(source, comparer);
}
}

Note that you really do want an extension method (or at least a generic method of some form) here, because you may not be able to express the type of T explicitly:

var query = from i in Enumerable.Range(0, 10)
select new { i, j = i + 1 };
var resultSet = query.ToHashSet();

You can't do that with an explicit call to the HashSet<T> constructor. We're relying on type inference for generic methods to do it for us.

Now you could choose to name it ToSet and return ISet<T> - but I'd stick with ToHashSet and the concrete type. This is consistent with the standard LINQ operators (ToDictionary, ToList) and allows for future expansion (e.g. ToSortedSet). You may also want to provide an overload specifying the comparison to use.

Optimize IEnumerable to HashSet conversion in LINQ

First, you don't need to enumerate the hashset values in your utilities function
you could improve the efficient by using nice static extension class written by @Jon

Converting linq result to hashset

and i think you don't need to check on the FirstOrDefault since the extension will handle the new student object given T
so you could change to more clean and tidy way.

IEnumerable<Student> studentTypes = this.studentTypes.Where(x => (x.studentID== studentId));
return studentTypes.toHashSet();

The other option is you can pass you IEnumerable into your constructor for HashSet
like

HashSet<Student> studentTypes = new HashSet<Student>(this.studentTypes.Where(x => (x.studentID== studentId)));

so you only have one line of code in your GetStudents function

How can I convert Linq results to DTO class object without iteration

The complete method could be:

public List<User> Get()
{
using (var db = new MyContext())
{
return (from u in db.Users
orderby u.FirstName
select new User()
{
Id = u.pkUser,
Username = u.Username,
Password = u.Password,
Active = u.Active
}).ToList();
}
}

You said you want the result "without iteration". Using LINQ also does not eliminate the iteration. You are not doing it in your code, but it really happens when you call the ToList() method.

Hashset check count before enumerating using Linq

Like this?

signalsobject.Aggregate("", (a, b) => a + " " + b)

How do I get results from a Linq query in the order of IDs that I provide?

I would just do it outside of the SQL query. There's really no benefit to getting it done on the SQL Server in this case.

var items = (from mytable in db.MyTable
where IDs.Contains(mytable.mytableID)
select mytable)
.ToArray()
.OrderBy(x => Array.IndexOf(ids, x.mytableID));

How do I copy projected results into another variable in c#?

There are two options.

First. Instead of using an anon data type, use Clients datatype. As in effect you are creating Clients object -

var clients = Clients.Where(c => c.FirstName.StartsWith("Mark"))
.Select(c => new Clients{
LastName = c.LastName.ToUpper(),
c.DateAdded,
c.FirstName,
})

Second. Create a list of object and assign whatever custom/anon data type to it -

var certainClients = new List<object> { };

Hashset vs. IQueryable

Yes, it is. Generally, IQueryable<T> implies that you are using a data source provider which is queried each time the queryable is enumerated (of course, this isn't always the case, as you can call AsQueryable extension method on an IEnumerable<T> which will give you an IQueryable<T> implementation over the IEnumerable<T> implementation).

To that end, storing the IQueryable<Results> in a dictionary doesn't actually prevent any hits to the data source when you enumerate through it a second time. It will make a request to the data provider every time you enumerate through it.

Because of this, you typically want to materialize the results on the client side, usually calling the ToList or ToArray extension methods, and then using IEnumerable<Results> or Results[] as the TValue type parameter of your dictionary.

Note that you could use a HashSet<T> to store your objects, but you have to make sure that you implement IEquatable<T> and override GetHashCode so that the default equality comparer will perform a comparison on the ID instance exposed by the Results type, either that, or you have to provide an IEqualityComparer<T> implementation that will do the same thing. It's more than likely that you are using designer-generated code, and it will not do this for you, and your objects will have equality determined by reference, not by value.

C# Linq Union Collections in nested query

You are possibly after something like this with SelectMany

Projects each element of a sequence to an IEnumerable and flattens
the resulting sequences into one sequence.

public ICollection<ConnectionHandler> GetConnectedClients(long tableId)
=> _gameTableEntries.Where(x => x.TableId == tableId)
.SelectMany(x => x.ConnectedClients)
.Where(x => x.IsConnected)
.ToHashSet();


Related Topics



Leave a reply



Submit