linq

Impact of using AsParallel() and AsSequential() in the same query? C#

半城伤御伤魂 提交于 2021-01-28 11:23:00
问题 I was going through PLINQ in one of the books and it said: If you have a complex query that can benefit from parallel processing but also has some parts that should be done sequentially, you can use the AsSequential to stop your query from being processed in parallel. For Example: var parallelResult = numbers.AsParallel().AsOrdered() .Where(i => i % 2 == 0).AsSequential(); I want to understand why is it allowed and what is the impact on the result? Is it running parallel? Is it running

LINQ Dynamic Expression Call the Count() method on a List property

旧巷老猫 提交于 2021-01-28 09:24:02
问题 Using LINQ Expression dynamic need to call the list Count() method. public class Foo { public int Id { get; set; } } public class Bar { public ICollection<Foo> Data { get; set; } } var list = new List<Bar>(); Need to build a predicate dynamically which can return me the list of Bar which have Data.Count() > 1 using Expressions. To start off something like this.. MethodInfo method = typeof(Enumerable).GetMethods() .Where(m => m.Name == "Count" && m.GetParameters().Length == 2).Single()

C# Linq Group by Object

帅比萌擦擦* 提交于 2021-01-28 09:11:09
问题 I have an issue of using group by in LINQ to SQL statement. The cod I have is var combinedItems = (from article in articles join author in authors on article.AuthorId equals author.Id into tempAuthors from tempAuthor in tempAuthors.DefaultIfEmpty() select new { article , author = tempAuthor}); var groups1 = (from combinedItem in combinedItems group combinedItem by combinedItem.article into g select g.Key).ToList(); var groups2 = (from combinedItem in combinedItems group combinedItem by

Use LINQ in C# to find MondoDB records when values in a list field match a criteria value from a list

对着背影说爱祢 提交于 2021-01-28 08:47:03
问题 I want to use LINQ to return all records in a MongoDB collection where the field in the record is a list of strings and any string in the list matches any string value in a list of strings used as the search criteria: Mongo Record in Collection ("Item"): { "_id": ..., "StringList": [ "string1", "string2", "string3" ], ... } Search Criteria: var criteria = new List<string> { "string2", "string4" }; My Code: var foundItems = iMongoDataProvider.FindAll<Item>() .Where(x =>x.StringList.ContainsAny

convert object list to type array and remove null values

寵の児 提交于 2021-01-28 07:58:00
问题 I want to get some data and convert it to a list of objects. From this list I want to select items that are not null and have a specific type. After that I want to convert these object variables to their correct type and return this list as an array. ContactEndpoint[] points = (GetContactInformation(contact, ContactInformationType.ContactEndpoints) as List<object>) .Select(item => item) .Where(item => item != null && item is ContactEndpoint) .ConvertAll(item => item as ContactEndpoint)

LINQ ternary result of binary list

时光总嘲笑我的痴心妄想 提交于 2021-01-28 07:47:56
问题 Assume I have a list of binary parameters (in reality it is a list of checkboxes' IsChecked.Value property). I'm trying to get the bool? (ternary) result that: is true if all the elements in the list are true is false if all the elements in the list are false is null in all other cases, thus there are both true and false elements in the list or the list is empty Until now I came up with the solution that requires iterating over the list twice (checking whether all elements are either true or

Linq: Elements not working when I have a namespace in XML file

社会主义新天地 提交于 2021-01-28 06:45:44
问题 I found some examples stackoverflow of using a xml to linq parser. My xml has a namespace, so I tried setting that as well (though I would of thought you could read that in a from the file?) Anyway, when I run the c#/linq code it does not recognise the elements in the xml. Unless I remove the xmlns tag from the xml file. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace ConsoleApplication2 { class Program { static void Main

Linq Includes Alternative

*爱你&永不变心* 提交于 2021-01-28 06:08:48
问题 I am a loading a entity from the database like so var profileEntity = await Context.Profiles .Include(x => x.MedicalRecords) .Include(x => x.DrugHistory) .Include(x => x.EmploymentStatus) .SingleOrDefaultAsync(x => x.Id == id); All is working fine, I was just wondering if there is a better way to include its non generic type properties rather using the Include method because this particular entity has a lot of properties I need to include 回答1: It is not possible to automatically eagerly load

Where Not in List<string>

雨燕双飞 提交于 2021-01-28 05:20:16
问题 I have a List<string> that contains a number of string values. Can I filter a collection when doing a query with a list instead of a single value? Something like this: List<string> slist = new List<string>() { "val 1", "val 2", "val 3" }; var q = (from s in ctx.Shipments where !s.ShipName.ToList().Contains(slist)).ToList() 回答1: What you need is !slist.Contains(s.ShipName) like: (from s in ctx.Shipments where !slist.Contains(s.ShipName) ).ToList(); You may see this article for creating IN

LINQ Sum of entries based on latest date

牧云@^-^@ 提交于 2021-01-28 04:57:19
问题 I have a table like so: Code | BuildDate | BuildQuantity --------------------------------- 1 | 2013-04-10 | 4 1 | 2014-09-23 | 1 1 | 2014-08-20 | 2 7 | 2014-02-05 | 4 I want the LINQ query to pick up the LATEST Build date for each Code, pick the BuildQuantity for that Code, and so on for all the records, and sum it up. So for the data above, the result should be 1 + 4 = 5 . This is what I'm trying: var built = (from tb in db.Builds orderby tb.BuildDate descending group tb by tb.Code into