linq

c# LINQ: Filter List of Files based on File Size

南楼画角 提交于 2021-01-28 01:01:08
问题 I need some help using LINQ to filer a list of files based on the file size. I have some code but it's using file.length instead of FileInfo(file).length. I don't know how to implement an object 'FileInfo' in the expression. HELP? { IEnumerable<string> result = "*.ini,*.log,*.txt" .SelectMany(x => Directory.GetFiles("c:\logs", x, SearchOption.TopDirectoryOnly)) ; result = result .Where(x => (x.Length) > "500000") ; } 回答1: You should be able to do something like this. Using a DirectoryInfo,

Convert.Int64 Is Not Reconized LINQ To Entities

狂风中的少年 提交于 2021-01-27 20:11:06
问题 I am getting the following exception: LINQ to Entities does not recognize the method 'Int64 ToInt64(System.String)' method, and this method cannot be translated into a store expression. I had long.Parse(ProjectID.ToString()) and I see the suggestion was to use Convert.ToInt64 but I am still getting the same exception string projID = ProjectFileID.ToString(); var d = (from f in context.FileInfo where f.ID == Convert.ToInt64(projID) select (f)); 回答1: Just do the conversion outside the query, so

RavenDB using filter with group by

故事扮演 提交于 2021-01-27 20:01:14
问题 I have a Transaction entity. I can make group by by (CustomerCode, CustomerName) then select CustomerCode and Total(Amount). It is easy. But When I Want to filter AtCreated. I have An Error. Unhandled exception. Raven.Client.Exceptions.InvalidQueryException: Raven.Client.Exceptions.InvalidQueryException: Field 'AtCreated' isn't neither an aggregation operation nor part of the group by key Query: from Transactions group by CustomerCode, CustomerName where AtCreated >= $p0 select CustomerCode,

dynamically build select clause linq

孤街醉人 提交于 2021-01-27 19:22:28
问题 class someClass { public int Id { get; set; } public string Name{ get; set; } ... public string someProperty { get; set; } } Expression<Func<someClass, object>> selector = null; selector = k => new { k.Id ,k.Name }; var serult = myData.Select(selector); // .Select(p=> new {p.Name , p.Id}) etc. This sample code is working But; Expression<Func<someClass, ???>> createSelector(string[] fields) { ... .... return ... } Expression<Func<someClass, ???>> selector = createSelector({"Name","Id"}); Is

Linq To Sql - Get week day name from date

孤人 提交于 2021-01-27 18:43:40
问题 Is it possible to construct a query in which I can retrieve week day name from a date into a separate column or variable? I know I can very easily do this on .NET side but would like it to be in the query. 回答1: You can also use SqlFunctions... var results=context.Listings .Select(l=>System.Data.Entity.SqlServer.SqlFunctions.DateName("dw",l.modify_date)); Of course, this only works when using a SQL Server. Methods that are cross-database, would be to use EntityFunctions.DateDiff with a known

Filter in EF Include [duplicate]

余生长醉 提交于 2021-01-27 18:40:49
问题 This question already has answers here : EF: Include with where clause [duplicate] (5 answers) Closed 2 years ago . I have this LINQ query that gives an error on the filter in the Include. When searching my friend Google I've found that it's not possible to filter in an Include. I've found some ways to do this on an onther way but I can't get it to work for my specific case. return context.Timesheets.Where(t => t.UserId == userId && t.SubjectDate == date && t.WorkingDaySchedules.Count() > 0)

Return new LINQ object

一曲冷凌霜 提交于 2021-01-27 17:50:17
问题 I want to write LINQ which return me new object(string, int) contains: string (position name) int (count of position) Output: PositionA 8 PostionB 12 PostionC 13 Here is what I have so far: public List<string, int> TestL() //or IEnumerable? { var q1 = TestList.GroupBy(s => s.Postion.ToUpper()) .Select(d => { return new { NameDisplay = d.Key, Count = d.Count(s => s.PersonNR) }; }) .OrderBy(g => g.Key); return q1; } TestList have fields like: Postion, PersonNR, City, LastName. All the fields

Compare two arrays using LINQ

落花浮王杯 提交于 2021-01-27 16:44:18
问题 For example, I have two arrays: string[] arrayOne = {"One", "Two", "Three", "Three", "Three"}; string[] arrayTwo = {"One", "Two", "Three"}; var result = arrayOne.Except(arrayTwo); foreach (string s in result) Console.WriteLine(s); I want Items from arrayOne which are not there in arrayTwo . So here I need result as: Three Three but I am getting no results as its treating "Three" as common and not checking the other two items("Three", "Three"). I dont want to end up writing a huge method to

Can't use .Count() from LINQ on Dictionary

若如初见. 提交于 2021-01-27 16:39:36
问题 When I try the following in VB.NET Dim data = New Dictionary(Of String, Integer) data.Count(Function(x) x.Value > 0) 'Compile-time error! I get this compile error with .Net Fiddle: Too many arguments to 'Public Overloads ReadOnly Property Count As Integer' Visual Studio gives me this error: 'Public ReadOnly Property Count As Integer' has no parameters and its return type cannot be indexed. The following does work though: Enumerable.Where(data, Function(x) x.Value > 0).Count() 'Works! data

Compare two arrays using LINQ

£可爱£侵袭症+ 提交于 2021-01-27 16:32:24
问题 For example, I have two arrays: string[] arrayOne = {"One", "Two", "Three", "Three", "Three"}; string[] arrayTwo = {"One", "Two", "Three"}; var result = arrayOne.Except(arrayTwo); foreach (string s in result) Console.WriteLine(s); I want Items from arrayOne which are not there in arrayTwo . So here I need result as: Three Three but I am getting no results as its treating "Three" as common and not checking the other two items("Three", "Three"). I dont want to end up writing a huge method to