linq-to-objects

In statement for LINQ to objects

我的未来我决定 提交于 2019-12-05 00:05:36
问题 Is there an equivalent of a SQL IN statement in LINQ to objects? 回答1: Yes - Contains. var desiredNames = new[] { "Jon", "Marc" }; var people = new[] { new { FirstName="Jon", Surname="Skeet" }, new { FirstName="Marc", Surname="Gravell" }, new { FirstName="Jeff", Surname="Atwood" } }; var matches = people.Where(person => desiredNames.Contains(person.FirstName)); foreach (var person in matches) { Console.WriteLine(person); } (In LINQ to SQL this ends up as an "IN" query.) Note that in LINQ to

How to use async lambda with SelectMany?

混江龙づ霸主 提交于 2019-12-04 22:36:12
I'm getting the following error when trying to use an async lambda within IEnumerable.SelectMany : var result = myEnumerable.SelectMany(async (c) => await Functions.GetDataAsync(c.Id)); The type arguments for method 'IEnumerable System.Linq.Enumerable.SelectMany(this IEnumerable, Func>)' cannot be inferred from the usage. Try specifying the type arguments explicitly Where GetDataAsync is defined as: public interface IFunctions { Task<IEnumerable<DataItem>> GetDataAsync(string itemId); } public class Functions : IFunctions { public async Task<IEnumerable<DataItem>> GetDataAsync(string itemId) {

Like query ing LINQ to Object

末鹿安然 提交于 2019-12-04 16:06:52
i have a US states list List<string> state // contain all 51 US states Now i have a string which contain some text like okl (it means Oklahoma for me). what i want i want 'like' query in List state and get Oklahoma state. Something like: var matches = states.Where(state => state.Contains(searchText)); That's fine if the case matches as well, but it doesn't work so well for case-insensitive matches. For that, you might want something like: var matches = states.Where(state => state.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) != -1); Choose the exact string comparison you want

Better performance on updating objects with linq

廉价感情. 提交于 2019-12-04 15:11:15
I have two lists of custom objects and want to update a field for all objects in one list if there is an object in the other list which matches on another pair of fields. This code explains the problem better and produces the results I want. However for larger lists 20k, and a 20k list with matching objects, this takes a considerable time (31s). I can improve this with ~50% by using the generic lists Find(Predicate) method. using System; using System.Linq; using System.Linq.Expressions; using System.Collections.Generic; namespace ExperimentFW3 { public class PropValue { public string Name;

How to a compose a Linq Expression to call OrderBy on a set of entities?

梦想与她 提交于 2019-12-04 12:22:18
Can someone explain the syntax for building an Expression that will OrderBy a user-specified property on an entity? This MSDN article goes a long way to helping, but it deals with a simple list of strings, my data set contains my own custom objects. http://msdn.microsoft.com/en-us/library/bb882637.aspx Code first, explanation later. IQueryable<T> data = this.Database.ObjectsOfType<T>(); var eachItem = Expression.Parameter(typeof(T), "item"); var propertyToOrderByExpression = Expression.Property(eachItem, propertyName); var runMe = Expression.Call( typeof(Queryable), "OrderBy", new Type[] {

Linq - SelectMany Confusion

人走茶凉 提交于 2019-12-04 07:29:39
问题 From what I understand from the documentation of SelectMany, one could use it to produce a (flattened) sequence of a 1-many relationship. I have following classes public class Customer { public int Id { get; set; } public string Name { get; set; } } class Order { public int Id { get; set; } public int CustomerId { get; set; } public string Description { get; set; } } I then try to use them using the query expression syntax like so var customers = new Customer[] { new Customer() { Id=1, Name =

Runtime creation of LINQ expression

对着背影说爱祢 提交于 2019-12-04 06:49:40
Say I have this expression: int setsize = 20; Expression<Func<Foo, bool>> predicate = x => x.Seed % setsize == 1 || x.Seed % setsize == 4; This basically 'partitions' a set of elements into 20 partitions and retrieves from each set each first and fourth element. This expression is passed to MongoDB which it's driver is perfectly capable of translating into a MongoDB "query". The predicate can, however, also be used on a list of objects (LINQ2Objects) etc. I want this expression to be reusable ( DRY ). However, I want to be able to pass in an IEnumerable<int> to specify which items to retrieve

Checking for duplicates in a complex object using Linq or Lambda expression

半城伤御伤魂 提交于 2019-12-04 03:49:57
问题 I've just started learning linq and lambda expressions, and they seem to be a good fit for finding duplicates in a complex object collection, but I'm getting a little confused and hope someone can help put me back on the path to happy coding. My object is structured like list.list.uniqueCustomerIdentifier I need to ensure there are no duplicate uniqueCustomerIdentifier with in the entire complex object. If there are duplicates, I need to identify which are duplicated and return a list of the

String.StartsWith not working with tilde (“~”) characters LINQ to SQL?

谁都会走 提交于 2019-12-04 03:47:35
问题 For some reason, my call to IEnumerable.Where() using String.StartsWith() appears to be giving different results depending on whether it's being used in LINQ-to-SQL or standard LINQ (-to-objects). If I add in a call to ToList() on what's otherwise the same call, I get different results back: var withToList = MyDataContext.MyEntities.ToList().Where(entity => entity.Name.StartsWith("~Test: My Test String")); // withToList.Count() returns 5, which is what I expect. var direct = MyDataContext

Can these two LINQ queries be used interchangeably?

走远了吗. 提交于 2019-12-04 03:43:55
问题 a) Would the following two queries produce the same results: var query1 = collection_1 .SelectMany(c_1 => c_1.collection_2) .SelectMany(c_2 => c_2.collection_3) .Select(c_3 => c_3); var query2 = collection_1 .SelectMany(c_1 => c_1.collection_2 .SelectMany(c_2 => c_2.collection_3.Select(c_3 => c_3))); b) I assume the two queries can't always be used interchangeably? For example, if we wanted the output elements to also contain values of c_1 and c_2 , then we only achieve this with query2 , but