[Solved] LINQ: Howto get person count by personid [closed]

Max : var biggestGrpOfPeopleHavingSamePersonId = people.GroupBy(x => x.personid).Max(x => x.Count()); Top : var top3peopleGroups = people.GroupBy(x => x.personid).OrderByDescending(x => x.Count()).Take(3); EDIT : The first query returns an element of type IGrouping<TKey,TValue> where TKey is of the same type of personid and TValue is of type Person. The second query returns an IEnumerable<> of objects like … Read more

[Solved] how should C# linq orderby thenby be used? [closed]

Your code has no ordering. What it has is code to generate a new query to order a list – but that is never executed. sortingList.OrderBy(m => m.RoleId) .ThenBy(m => m.ToolbarLocation) .ThenBy(m => m.Band) .ThenBy(m => m.BandIndex); The return of these calls is an IQueryable that has to be executed. You never do .OrderBy etc. … Read more

[Solved] Generating list in a following format [closed]

Something like this would work var results = from x in context.Tests group x by new { x.DeptId, x.StudentId, x.TeacherId } into grp select new { grp.Key.DeptId, grp.Key.StudentId, grp.Key.TeacherId, A = grp.FirstOrDefault(x => x.TestName == “A”)?.TestValue, B = grp.FirstOrDefault(x => x.TestName == “B”)?.TestValue }; This requires C# 6 for the null-conditional operator ?., but you … Read more

[Solved] How can I merge this two bits of code [closed]

Do you want something like this? genOutList= ( from i in genOutList where !genAccList.Any(x=>x.Contract==i.Contract) select i ).ToList(); Or genOutList.RemoveAll(x=>genAccList.Any(i=>i.Contract==x.Contract)); 0 solved How can I merge this two bits of code [closed]

[Solved] Setting variable types from CSV

Right… Because you can never know what the input will be from the CSV you can’t always try and implicitly convert the string to a DateTime and because you want to do the convert of a string within the select a way to do this is an extension method. public static class StringExtensions { public … Read more

[Solved] How to use WHERE in LINQ? [closed]

I think you should try using CopyToDataTable ds.Table[0].AsEnumerable().Where<DataRow>(r=>r.Field<int>(“productID”)==23).CopyToDataTable(); When you convert it to an Enumerable the filtered list has no identifiers for say ProductID hence the grid would fail to map the column and fail. Alternatively you may also choose to use AsDataView() instead of CopyToDataTable(). A more detailed explanation can found Binding DataRows Databinding … Read more

[Solved] c# round to two decimal places [closed]

Please add excepted result and the purpose on your question. Check this: List<decimal> abc = new List<decimal> { 500, 500 }; List<decimal> abcd = new List<decimal> { 12, 100 }; var cd = string.Join(“,”, abc.Zip(abcd, (q1, q2) => Math.Round(q2 / q1 * 100.00M, 2))); solved c# round to two decimal places [closed]

[Solved] sql to linq conversion for this table [closed]

You can do something like this:- var output= countries.GroupBy(c => new { c.State, c.City }) .Select(a => new { City = a.Key.City, State = a.Key.State, Zip = a.Max(z => z.Zip) }).OrderBy(m => m.City); 1 solved sql to linq conversion for this table [closed]

[Solved] How to get rows with most recent nulls?

I’m not so familiar with Linq-To-Sql, so i’m not sure if it is supported, but try: var query = db.TableName .Where(r1 => r1.Col2 == null && r1.Col1 < db.TableName .Where(r2 => r2.Col1 != null) .Select(r2 => r2.Col1) .OrderBy(col1 => col1) .FirstOrDefault()); At least it should be the LINQ equivalent of this (working) sql-query: http://sqlfiddle.com/#!6/439be/6/0 solved … Read more

[Solved] SQL Linq syntax [closed]

One way would be like this: db.polls.Where(p => p.id == polls.Where(x => x.publish_at == polls.Max(y => y.publish_at)).Max(x => x.id)); Another way like this: from p in db.polls where p.id == (from x in db.polls where x.id == (from y in db.polls where y.publish_at == db.polls.Max(y => y.publish_at) select y.id).Max()) select x.id).Max()) select p; 1 solved … Read more

[Solved] Can’t use ‘contains’ in LINQ [closed]

You are facing problem on this line (result => result.SiteUrl.Contains(last.ToString()); Can you please check that SiteUrl is type of string otherwise it not going to work for you. because last is type of string and Contains is method supported by string type … or otherwise last need to be enumebrable collection and siteurl also enumerable … Read more