linq-to-objects

Why does LINQ query throw an exception when I attempt to get a count of a type

≯℡__Kan透↙ 提交于 2019-12-06 17:33:23
问题 public readonly IEnumerable<string> PeriodToSelect = new string[] { "MONTH" }; var dataCollection = from p in somedata from h in p.somemoredate where h.Year > (DateTime.Now.Year - 2) where PeriodToSelect.Contains(h.TimePeriod) select new { p.Currency, h.Year.Month, h.Value }; Can someone tell me why an exception is thrown when at the following line of code? int count = dataCollection.Count(); This is the exception: System.NullReferenceException: Object reference not set to an instance of an

Better performance on updating objects with linq

笑着哭i 提交于 2019-12-06 11:45:56
问题 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

Like query ing LINQ to Object

戏子无情 提交于 2019-12-06 11:34:29
问题 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. 回答1: 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

Return Max value with LINQ Query in VB.NET?

*爱你&永不变心* 提交于 2019-12-06 10:57:52
I have a method that takes in an id, a start date, and and end date as parameters. It will return rows of data each corresponding to the day of the week that fall between the date range. The rows are all doubles. After returning it into a DataTable, I need to be able to use LINQ in VB.NET to return the maximum value. How can I achieve this? Here is the initial setup? Dim dt as DataTable = GetMaximumValue(1,"10/23/2011","11/23"/2011") 'Do linq query here to return the maximum value The other alternative is to just return a Maximum value just given an id which would be slightly easier to

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

断了今生、忘了曾经 提交于 2019-12-06 08:59:26
问题 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 回答1: Code first, explanation later. IQueryable<T> data = this.Database.ObjectsOfType<T>(); var eachItem = Expression.Parameter(typeof(T), "item"); var propertyToOrderByExpression = Expression

How might I complete this example using LINQ and string parsing?

爷,独闯天下 提交于 2019-12-06 03:46:51
问题 I'm trying to write a simple program that will compare the files in separate folders. I'm currently using LINQ to Objects to parse the folder and would like to included information extracted from the string in my result set as well. Here's what I have so far: FileInfo[] fileList = new DirectoryInfo(@"G:\Norton Backups").GetFiles(); var results = from file in fileList orderby file.CreationTime select new { file.Name, file.CreationTime, file.Length }; foreach (var x in results) Console

How to do If statement in Linq Query

雨燕双飞 提交于 2019-12-06 01:11:41
I currently have a list that contains the following CountryCode (string) CountryStr (string) RegionStr (string) RegionID (int) AreaStr (string) AreaID (int) This is a flattened set of linked data (so basically the results of a joined search that ive stored) The MVC route will only pass one string which I then need to match up to the data at the right level in the heirachy. So I'm trying to query the CountryStr then if it doesn't produce results the region then the area; but I need to do that bit of the query and for instance... var datURL = (from xs in myList //query 1 where xs.RegionStr ==

LINQ: Self join query, how to accomplish this?

╄→尐↘猪︶ㄣ 提交于 2019-12-05 23:54:56
Can anyone help? I have 1 class, basically it holds Members and within that class is a List. The members i have in a List also... So basically it goes like this, I have 2 members and each member has a number of sessions. I wish to only return each member with 1 Session. I have done a LINQ query, but of course it doesn't work... I think i need to do a self join, any ideas? Basically my error is m doesn't exist in my subquery self join. var sessions = from m in this.members join s in ( from se in m.Sessions group se by se.Name into g select new {Name = g.Key, SessioEndTime = g.Max(a=>a

Dynamic LINQ Queries

你。 提交于 2019-12-05 21:54:58
Is it possible to create Linq Queries at runtime. Using an xml rule which can be translated to a Linq Query. Ultimately, yes; but it isn't simple and you'll need to either: learn the Expression API use the pre-rolled dynamic LINQ library (from the samples download) If you want to go the first option, then you need to create your own lambdas; imagine, for example, that you have something like (making things up here...): <Filters> <Add Prop="Foo">My filter value</Add> </Filters> Then you would need to do something like: XElement filters = ...; // the "Filters" element IQueryable<Customer> query

Use string as field name in LINQ

孤人 提交于 2019-12-05 19:13:35
问题 Look the code below. I'd like to replace USERNAME by the field name received in the parameter field . This method must be able to make some search on several fields. Thank, public void Searching(string field, string stringToSearch) { var res = from user in _dataContext.USERs where user.USERNAME.Contains(stringToSearch) select new { Id = user.ID, Username = user.USERNAME }; } 回答1: You need to forget about the anonymous type, maybe use Tuple<int,string> instead; but: how about: IQueryable<Foo>