Get the next and previous sql row by Id and Name, EF? - c#

Assume we have the following data in a SQL Server table (sorted by name) :
Id Name LName
-------------------------
5 Abbas Dayyan
3 Mohammad KD
4 Nima Ahmad
1 Omid Zangene
2 Pedram Ahmadi
we have an Id query string and we wanna get the next and previous row (if exists) from Id.
e.g :
the Id query string is 4, so we wanna get Mohammad KD as previous row and Omid Zangene as next row.
Could you please guide me how can do it with LINQ to Entity Framework.
Edit:
In practice the number of table rows is around 1 million.
Table rows didn't sort by Name by default, wa need to sort them by Name for the result.

How about this?
var result = (from person in db.People
where person.Id == id
let ordered = db.People.OrderBy(p => p.Name)
let reversed = db.People.OrderByDescending(p => p.Name)
let previous = reversed.SkipWhile(p => p.Id != id).Skip(1).FirstOrDefault()
let next = ordered.SkipWhile(p => p.Id != id).Skip(1).FirstOrDefault()
select new { previous, next }).First();
Edit: Took the new specifications into account.
Edit 2: Modified the code to not use LastOrDefault, which doesn't work with LINQ to Entities.

Try this:
int id = 4;
var list = (from x in ctx.Table
where x.id >= id - 1 && x.id <= id + 1
select x).OrderBy(o -> o.name).ToList();
edit: this will return elements with ID 3,4 and 5. I dont know if you are actually ordering the table in the database by name, but if you are i think it would be easier to just use a loop instead of using linq to get the previous and next element.
update: sorted the result by name

Related

c# linq to entities using method based queries - trying to select where the object appears only once

i have got this table that relates the Table hardware with a table Process..
this table is called processHardware.
this table is discribed by:
IDProcessHardware
IDProcess
IDHardware
State
the field state can have 3 states (1-Insert, 2-Remove,3-Substitute)..
so i can i have this:
IDProcessoHardware IDProcesso IDHardware State
1 10 1 1
2 10 2 1
3 10 1 2
what this tell me is that the hardware with id 1 was insert on the process with the id 10
then the user insert the hardware with id 2 on the process with the id 10, and the it remove the hardware with the id 1 from the process with the id 10
by giving the id of the process i want to get the id of the hardware that were insert, this is, the id of the hardware that were remove..
so in this case the record that i will get is record number 2..because was insert, but was not removed..
after getting the ids from this table i need to relate the ids with the table hardware, this table is described by idhardware, serial number, description..
i was using linq method base..
and this was something that i did, but didnt go further after this..
var ProcessoHardware = from procHardware in db.ProcessoHardwares
where procHardware.Rem == 0 && procHardware.IDProcesso == IDProcesso
group procHardware by procHardware.IDHardware into g
select new { IDHardware = g.Key, count = g.Count() };
the query above didnt work for me...
so i want to get the records that appears only once on the table, and then relate the ids that were obtained from this query and get the info about those ids like, serial number, description(these fields are on a table called Hardware).
thanks in advance..
in sql i manage to do the query ..
SELECT *
FROM
(SELECT IDHardware ,COUNT(IDHardware) nu
FROM dbo.ProcessoHardware
WHERE IDProcesso=47
Group By IDHardware) T WHERE nu=1
how do i pass this to linq?
Firstly your SQL statement would be clearer if you used the having clause so it becomes
SELECT IDHardware, COUNT(IDHardware) nu
FROM dbo.ProcessoHardware
WHERE IDProcesso=47
GROUP BY IDHardware
HAVING COUNT(IDHardware) = 1
secondly, your SQL statement doesn't mention a field called Rem, but your LINQ states where procHardware.Rem == 0. I'm going to assume that you need to keep that filter. If so then all you need to do is add a where clause to count your group, g. Try the following
var ProcessoHardware = from procHardware in db.ProcessoHardwares
where procHardware.Rem == 0 && procHardware.IDProcesso == IDProcesso
group procHardware by procHardware.IDHardware into g
where g.Count() == 1
select new { IDHardware = g.Key, count = g.Count() };
although the literal transformation of your statement (without the Rem and hard coded ID of 47) to LINQ would be
var ProcessoHardware = from procHardware in db.ProcessoHardwares
where procHardware.IDProcesso == 47
group procHardware by procHardware.IDHardware into g
where g.Count() == 1
select new { IDHardware = g.Key, count = g.Count() };

Linq List Contains Method

Im newbie to linq and im using linq query to retrieve data from the table.My idea is to list all the cashsafes corresponding to a particular user and show it in dropdownlist.
The table structure is shown below
Table 1
cashsafeid cashsafename
1 cashsafe1
2 cashsafe2
3 cashsafe3
Table 2
Id UserId Cashsafeid
1 100 1,2,3
2 101 1,3
I've to get the cashsafename of a particular user say 100.How can i achieve it
The below code is the one i've tried but am stuck
List<Cashsafe> cashsafes=(from c in db.Table 1
where c.CashsafeId contains() )--Cannot go further
You store User's Cachsafeid column in very inefficient way - it doesn't allow to generate efficient SQL for LINQ provider. So the following solution has bad performance - if you care about that - change your table structure.
var user = db.Table2.Single(u => u.UserId == 100);
var cachfeIds = user.Cashsafeid.Split(',').Select(int.Parse).ToArray();
var cachefes = db.Table1.Where(c => cachfeIds.Contains(c.Id)).ToList();
Basically you need to join to tables, but foreign key is "virtual" - it is only in your mind. To retrieve foreign key values we must split the Cachsafeid column's value of every user to retrieve linked cachefes. And only then retrieve the cachefes with separate request (I think LINQ will retrieve all values from table and the execute Where part in C# code).
if you have no idea of join you can use
int x = 0;
List<int> Users = db.table2.FirstOrDefault(m => m.UserId == 100).Cashsafeid.Split(',').ToList().Where(str => int.TryParse(str, out x)).Select(str => x).ToList(); ;
var content = db.table1.Where(m => Users.Contains(m.cashsafeid)).ToList();

How to select and exclude records from the same subset in LINQ

I have a sample connection table PolicyToX with fields Id, PolicyId, PersonId, SchoolId. Records are always saved with one of the FKs being NULL, for example 1, 1, 5, NULL.
I want to write a query in LINQ that, when given two parameters: PersonId and SchoolId will filter all Policies of the given School but without those that are already bound to a given Person.
So, if I have a dataset of:
[Id][PolicyId][PersonId][SchoolId]
1 1 5 NULL
2 1 NULL 1
3 2 NULL 1
and pass paremeters PersonId = 5 and SchoolId = 1 the result should be one Policy of ID = 2.
Thanks!
Assuming PolicyToX contains the data then is this what you're looking for?
var ids = from e in PolicyToX where e.PersonId == personId select e.PolicyId;
var result = from d in PolicyToX where d.SchoolId == schoolId && !ids.Contains(d.PolicyId) select d;
var data = list.Where(x => x.SchoolId == schoolId && x.PersonId != personId);
Are you talking about this?

EF Sum between 3 tables

Say we got a Database design like this.
Customer
Id Name
1 John
2 Jack
Order
Id CustomerId
1 1
2 1
3 2
OrderLine
Id OrderId ProductId Quantity
1 1 1 10
2 1 2 20
3 2 1 30
4 3 1 10
How would I create an entity framework query to calculate the total Quantity a given Customer has ordered of a given Product?
Input => CustomerId = 1 & ProductId = 1
Output => 40
This is what I got so far, through its not complete and still missing the Sum.
var db = new ShopTestEntities();
var orders = db.Orders;
var details = db.OrderDetails;
var query = orders.GroupJoin(details,
order => order.CustomerId,
detail => detail.ProductId,
(order, orderGroup) => new
{
CustomerID = order.CustomerId,
OrderCount = orderGroup.Count()
});
I find it's easier to use the special Linq syntax as opposed to the extension method style when I'm doing joins and groupings, so I hope you don't mind if I write it in that style.
This is the first approach that comes to mind for me:
int customerId = 1;
int productId = 1;
var query = from orderLine in db.OrderLines
join order in db.Orders on orderLine.OrderId equals order.Id
where order.CustomerId == customerId && orderLine.ProductId == productId
group orderLine by new { order.CustomerId, orderLine.ProductId } into grouped
select grouped.Sum(g => g.Quantity);
// The result will be null if there are no entries for the given product/customer.
int? quantitySum = query.SingleOrDefault();
I can't check what kind of SQL this will generate at the moment, but I think it should be something pretty reasonable. I did check that it gave the right result when using Linq To Objects.

LINQ association - Entity Framework

I have three tables and I have used edmx designer to add associations between them. Below is how they are linked.
(table1) Loans - (table 2) Investor : Many to One relationship
(Table2) Investor - (Table3) InvestorInfo : One to Many relationship
I want to get [1] Total loans count sold to one investor, [2] Investor name and [3] investor's service fee which is stored in Table3 at idx = 2005 for each investor ("investor id & idx" is primary key of table3 - InvestorInfo table).
How do I do that in below query? I am forced to select 'FirstOrDefault()' to access any column in Table3 (See commented lines). If I use FirstOrDefualt, I get a record where idx = 1 and not 2005.
var loanPurchaseData = (from cd in entity.Table1
//where cd.Table2.Table3.Select(x => x.IDX == 2005)
//where cd.ULDD_SET_POOLS.ULDD_SET_POOLDT.FirstOrDefault().SORT_ID == 2005
group cd by new { cd.Table4.PurchaseDate, cd.Number } into grp
select new
{
investor = grp.FirstOrDefault().Investor,
no_of_loans = grp.Count(),
sort_id = grp.FirstOrDefault().Table2.Table3.FirstOrDefault().SORT_ID,
service_fee_rate = grp.FirstOrDefault().Table2.Table3.FirstOrDefault().DT_REAL_PERC_VALUE
}).ToList();
Your question is not very clear - I don't understand whether idx is in Table3 or Table1, and what you want to select, but I will assume you have a many-one-many schema, where an Investor has zero or more Loans & zero or more InvestorInfos. You want to get, say, all of the loans which join to investor info with idx = 2005. Correct me if I'm wrong, and correct your question if I'm right!
Starting with your InvestorInfo object, you know that there's only one Investor but there will be zero or more Loans.
// only one InvestorInfo for idx, but this isn't clear in your question
var investorInfo = context.InvestorInfos.SingleOrDefault(i => i.idx == 2005);
var loans = investorInfo.Investor.Loans;
The crux of your problem is that you cannot get 'the loan service fee' for an investor info. Why not? Because that investor has 5 loans. Which one do you want?
-- we can get the maximum, minimum, sum, etc...
var max = loans.Max(l => l.DT_REAL_PERC_VALUE);
var min = loans.Min(l => l.DT_REAL_PERC_VALUE);
var min = loans.Sum(l => l.DT_REAL_PERC_VALUE);
Again it's not clear what you are trying to do, nor what your data actually looks like, but in a one-many relationship you will necessarily have more than one of the 'many' side for each of the 'one' side.
To get the max, use the Max operator.
service_fee = grp.Max(l => l.Table2.Table3.Max(t => t.DT_REAL_PERC_VALUE))

Categories