finding multiple row indexes from a datagridview with linq - linq

the below answer gets me the first row index but there are cases where there are multiple rows with my where clause. how do i get a "record set" where there multiple rows.
int index = -1;
DataGridViewRow rowx =
dgvMatchErrors.Rows.Cast<DataGridViewRow>()
.Where(r => r.Cells[1].Value.ToString().Equals(intErrorRowid.ToString()))
.First();
index = rowx.Index;
How to find the row ID from datagridview, given a row value?

You can try by removing First() method call in query
DataGridViewRow rowx =
dgvMatchErrors.Rows.Cast<DataGridViewRow>()
.Where(r => r.Cells[1].Value.ToString().Equals(intErrorRowid.ToString())).Select(r=>r);

This gives me a list of all my row index based on my where clause.
List<DataGridViewRow> rows =
dgvMatchErrors.Rows.Cast<DataGridViewRow>().Where(
r => r.Cells[1].Value.ToString().Equals(intErrorRowid.ToString())
).ToList();

Related

Linq query to select all records that have sub records that match all values in an array of values?

I have a table that contains records that have a related table that may have multiple records for each of the main records. This is a table of flags.
i.e.
MyRecord can have multiple subrecords Flag1, Flag4, Flag7
I am trying to create a Linq query that will return records that have all the flags I specify.
This is code of what I am trying to do
var flags = new List<string>() { "Flag1"};
db.Contents.Include(f=>f.Flags)
.Where(a => flags.All(b=>a.Flags.Any(f =>f.Name==b)));
The above almost works but returns records with Flag1 along with records that have Flag1,Flag4 and Flag1,Flag4,Flag7. I need to return only records that have the requested flags set.
var answer = db.Contents.Include(x => x.Flags)
.Where(x =>
x.Flags.Select(y => y.Name).Distinct().Count() == flags.Count
&&
flags.All(y => x.Flags.Any(z => z.Name == y)))
.ToList();

LINQ select column(s) of table before doing multiple joins

After running the query below and hovering over "usersToWork" when debugging, I can view all of the properties of the single entry that I get returned to me in addition to the other tables that have relations to this value. What I need to display to the user is the "Lines.Id" (Lines being the table and Id being the column in the Lines table) value, however that value gets lost from the SelectMany() statements. Is there anyway to select that "Lines.Id" value to include in the final value that I get from all of my joins? In the code below, I commented out what I want but I can't place that there otherwise I get error on the first SelectMany statement saying 'int' does not contain a definition for 'Shifts' and no extension method 'Shifts' accepting a first argument of type 'int' could be found.'
Correct me if I'm wrong but SelectMany() selects all of the columns from what you want to join on. In this case, in the first SelectMany() I get only values from the "Shifts" table and in the second SelectMany() I get only values from the "Users" table. Why is this different from the SQL join? When joining in SQL you can get every column as you join them together, SelectMany() yields only the values of the second table that you are joining on. Is it even possible to get that value in the "Lines" table or will I have to do another query? Any help would be great.
int idEnteredByUser = 123;
var usersToWork = entityDataModel.Lines
//....NOT IN MY CODE NOW....
// .Select(line => line.Id)//THIS IS WHAT I NEED.
// .Select(line => line.Description, line.Id//OR THIS TO RETURN TWO VALUES IF POSSIBLE
//This is my current code, I need to include on of the select lines above.
.SelectMany(line => line.Shifts) //Join lines on shifts.
.Where(shift => shift.EndTime >= DateTime.Now) //Join restricted times.
.SelectMany(user => user.Users) //Join the restricted shift times on users.
.Where(user => user.UserId == idEnteredByUser ); //Only look for the specific user
This works much easier using LINQ query syntax.
I'm assuming that you made a typo in your posted code and that user is a property of shift.
var idEnteredByUser = 123;
var usersToWork =
from line in entityDataModel.Lines
from shift in line.Shifts
where shift.EndTime >= DateTime.Now
from user in shift.Users
where user.UserId == idEnteredByUser
select new
{
Description = line.Description,
Id = line.Id
};

linq query with count

I would like to create a simple linq query, but I don't really know, how it should be. I have searched the net, but found nothing, what I can use or I don't know yet, that I could use it.
So, basically, I have a table with this fields: reference, vat_code, amount, vat_amount, supplier.
Now, I would like to query the records, where reference<>'' and the reference is more than once in the table. But I need all the occupians.
F.e. from
1;VF;100;27;345
2;VF;200;54;123
2;VF;-200;-54;123
2;VF;200;54;123
3;VF;300;81;888
to
2;VF;200;54;123
2;VF;-200,-54;123
2;VF;200;54;123
How would be look the linq query to this?
Thanks.
If rows with same reference should go together in results, then you should filter out rows with reference equal to empty string, then group all rows by reference and select only those groups which have more than one row.
C# sample with DataTable:
var result = dt.AsEnumerable()
.Where(r => r.Field<string>("reference") != "")
.GroupBy(r => r.Field<string>("reference"))
.Where(g => g.Count() > 1)
.SelectMany(g => g); // flatten group to sequence of rows

Summing and grouping in datatable

1.Requirement:
the above is the result of my datatable value
1. Need to group the states by summing the premium value
2. need to mention the address, city for the states which is having highesht premium values
3. Linq or for loop anything is fine for me, please help me out
thanks,
Srikanth Anantharaman
You could use this LINQ query to fill a second table:
DataTable newTable = tbl.Clone(); // empty table, same schema
var groupedByState = tbl.AsEnumerable()
.GroupBy(r => r.Field<string>("State"));
foreach(var group in groupedByState)
{
DataRow maxPremRow = group.OrderByDescending(r => r.Field<int>("Premium")).First();
DataRow newRow = newTable.Rows.Add();
newRow.SetField("State", group.Key);
newRow.SetField("Address", maxPremRow.Field<string>("Address"));
newRow.SetField("City", maxPremRow.Field<string>("City"));
newRow.SetField("Premium", group.Sum(r => r.Field<int>("Premium")));
}

JoinQueryOver in QueryOver to select notin list

I have a list of record that I picked up through the code:
var list= NhSession.QueryOver<Data.Models.MembModel>()
.Where(w => w.Comp.Id == idcomp)
.JoinQueryOver(jq => jq.Ver)
.Select(s => s.Ver)
.List<Dados.Models.VerModel>();
With this code I get a list of VerModel that I have relation in a MembModel. The problem is that I what get the list of VerModel that don't be in relation in a MembModal, I think to describe this, I want to select one list that is "notin" a first list. How can I do this?
Tks
What we need, as you said, is a NOT IN (subquery) statement. And NHibernate does have a clear way how to achieve that. First the subquery, which will return MembModel collection (filtered or not - as needed), represented by the VerModel.ID
var subquery = QueryOver.Of<Data.Models.MembModel>()
// we can still filter this sub-select ... or not
// .Where(w => w.Comp.Id == idcomp)
// what we need to be returned is the reference id, the VerModel.ID
.Select(m => m.Ver.ID);
And now we will query the VerModel itself, with the NOT IN (subquery) clause:
var list = session.QueryOver<Dados.Models.VerModel>()
.WithSubquery
.WhereProperty(v => v.ID) // the ID to match the prev selected one
.NotIn(subquery) // should NOT be IN
.List<Dados.Models.VerModel>();
Check:
16.8. Subqueries

Resources