linq group by statement not working - c#

I have a linq statement like following,
var v1 = from s in context.INFOONEs group s by s.DATET into xyz select xyz;
I am trying like following to display the results
foreach (var x in v1)
{
Console.WriteLine(x.);
}
But intellisence is not showing the columns when I type x.
What I am doing wrong? And what is the right way to achieve what I am trying to accomplish?
Thanks

because there is no column in x. There are some record(s) under each group.So you need to get those records:
foreach (var x in v1)
{
Console.WriteLine(x.Key); // display the Key of current group
foreach(var item in x) // iterate over the records in the group
{
Console.WriteLine(item.) // here you can access your columns
}
}

Related

How to simplify LINQ query just to filter out some special rows

I am very unfamiliar with Entity Framework and LINQ. I have a single entity set with some columns where I want to filter our some special rows.
4 of the rows are named Guid (string), Year (short), Month (short) and FileIndex (short). I want to get all rows which have the maximum FileIndex for each existing combination of Guid-Year-Month.
My current solution looks like this:
var maxFileIndexRecords = from item in context.Udps
group item by new { item.Guid, item.Year, item.Month }
into gcs
select new { gcs.Key.Guid, gcs.Key.Year, gcs.Key.Month,
gcs.OrderByDescending(x => x.FileIndex).FirstOrDefault().FileIndex };
var result = from item in context.Udps
join j in maxFileIndexRecords on
new
{
item.Guid,
item.Year,
item.Month,
item.FileIndex
}
equals
new
{
j.Guid,
j.Year,
j.Month,
j.FileIndex
}
select item;
I think there should be a shorter solution with more performance. Does anyone have a hint for me?
Thank you
You were close. It's not necessary to actually select the grouping key. You can simply select the first item of each group:
var maxFileIndexRecords =
from item in context.Udps
group item by new { item.Guid, item.Year, item.Month }
into gcs
select gcs.OrderByDescending(x => x.FileIndex).FirstOrDefault();

Create a LINQ query comparing arrays

I'm very new to LINQ and I'm searching to filter an SQL view which has a column called 'Readers' containing several group names separated by '#' (es. "Administrators#HR Group#Employees Group").
Having a list of user's groups, I need to extract all the records where Readers contains at least one of the user's groups.
In other words, the user must see only those records belonging to him.
I found this solution, but I think it's extremely inefficient:
private List<vwFax>getmyFaxes(List<string> myGroups)
{
var myFax = db.vwFax.AsQueryable();
var res = db.vwFax.AsQueryable();
List<vwFax> outRes= new List<vwFax>();
foreach (string elem in myGroups)
{
res = (from a in myFax
where a.Readers.Contains(elem)
select a);
if(res.Count() > 0)
{
outRes.AddRange(res);
}
}
return outRes.ToList();
}
Any help please?
Basically what you are saying in the following query is: For each item in myFax take it only if that item.Readers contains Any (at least 1) of the items in myGroups
outRes = db.myFax.Where(item => myGroups.Any(grp => item.Readers.Contains(grp)));
and in query-syntax:
outRes = from item in db.myFax
where myGroups.Any(grp => item.Readers.Contains(grp))
select item;

c# Using Select Linq statements?

I have Rows object that is IEnumerable<dynamic>, it has 5 properties (columns) and 100 rows. One of the properties/columns is Group, only 2 distinct groups out of 100 rows, so first I run a distinct against it:
IEnumerable<dynamic> Groups = Rows.Select(x => x.Group).Distinct();
This works, no error.
Then I want to go back to my Rows object and loop through them where this group = the group in Rows, like this:
foreach (string Group in Groups)
{
IEnumerable<dynamic> GroupData =
from rowdata in Rows
where rowdata.Group = #Group
select rowdata;
But I get this error on the last line:
'WebMatrix.Data.DynamicRecord' does not contain a definition for 'Group'
Anyone knows why this isn't working?
Surely I can do this another way, but I wanted to use c# select statement instead. How can I though?
Edit to show usage:
foreach (var row in GroupData){
string ThisGroup = row.Group
}
...
Instead of selecting twice, group on the Group value:
IEnumerable<IGrouping<string, dynamic>> groups = Rows.GroupBy(x => (string)x.Group);
Now you can just loop through the result:
foreach (IGrouping<string, dynamic> group in groups) {
...
}
The IGrouping<> object has a Key property which is the value that you grouped on, and it's also a collection of the values in the group.

Creating a Linq group by query

//SELECT table1.GG_ITEM, Sum(table1.REM_QTY) AS SumPerGG_ITEM
//FROM table1
//WHERE (table1.SUGG_DOCK_DATE Is Not Null)
//GROUP BY table1.GG_ITEM
//ORDER BY table1.GG_ITEM;
var try1 = (from row in db2.Dumps select new { Type1 = row.GA_ITEM, Type2 = row.REM_QTY });
Debug.Print(":::::try1:::::");
foreach (var row in try1)
{
Debug.Print(row.Type1.ToString());
Debug.Print(row.Type2.ToString());
}
var try2 = (from row in db2.Dumps group row by row.GA_ITEM into g select new { Type1 = g.Key, Type2 = g.ToList() });
Debug.Print("::::try2:::::");
foreach (var row in try2)
{
Debug.Print(row.Type1.ToString());
Debug.Print(row.Type2.ToString());
}
I'm converting an Access SQL query to Linq. The two columns I am selecting from my table Dumps are GA_ITEM and REM_QTY. My try1 is working out just fine and I see the contents of both columns printed out. My try1 is not yet duplicating the functionality of the Access SQL query.
My try2 is an attempt at grouping. For my try2 row.Type1.ToString() is readable however row.Type2.ToString() is showing up in the output window as:
System.Collections.Generic.List`1[garminaspsandbox3.Models.Dump]
What I really would like to do is in try2 select GA_ITEM and REM_QTY like I did in try1 and group by GA_ITEM however those fields aren't showing up in my autocomplete for the g object.
Does anyone know how to do this in Linq?
Thank you for posting...
Your Type2 property holds a List, not a single item,So you need to use another loop and iterate over the items in that group:
foreach (var row in try2)
{
Debug.Print(row.Type1.ToString());
foreach(var item in row.Type2)
{
Debug.Print(item.GA_ITEM);
Debug.Print(item.REM_QTY);
}
}

Returning the correct type in C# linq query over DataRows

I can't seem to get the following working
var results = (from DataRow row in myDataView.Table.Rows
group row by row["part"] into x
select new {x}).Distinct();
foreach(var x in results)
{
doSomething(x["part"]);
doSomethingElse(x["field2"]);
}
The problem seems to be that results contains elements of "AnonymousType" which I guess is an artifact of "group". How can I make the above code work as I expect?
new {x}
this will create an anonimous type with property 'x' where x object will be stored. This is short syntax for:
new {x = x} // first x is property name
in your case you should simply select x:
var results = (from DataRow row in myDataView.Table.Rows
group row by row["part"] into x
select x).Distinct();
Also Distinct() after Group looks strange. What did you mean?

Categories

Resources