C# LINQ Lambda query with select, Where, Count and Distinct - c#

I am brand new to linq and i need help with a query. I have 3 columns in a datatable. I need to get a count of unique values from col3, where col1 and Col2 contain certain values. here is the last piece of code i tried, but it is not working. can someone please help me with this?
thanks
AD = dt.AsEnumerable()
.Where(x => x.Field<string>("Col1").Equals("Value1")
|| x.Field<string>("Col2").Equals("Value2"))
.Select(s => s.Field<string>("Col3")
.Distinct().Count());

I'm missing a closing parenthesis at .Select(s => s.Field<string>("Col3"), try this:
AD = dt.AsEnumerable()
.Where(x => x.Field<string>("Col1").Equals("Value1")
|| x.Field<string>("Col2").Equals("Value2"))
.Select(s => s.Field<string>("Col3")) // <-- add this
.Distinct().Count(); // <-- remove this

var q1 = (from persons in _db.Table<Person>()
where persons.firstName==fname && persons.lastName==lname group
persons.age by persons.age ).Count();
I 've tested this on some "test" cases using sqlite-net library - and it seems to work.
Although there is already a solution - they are using different styles , so I decided to post it there to

Related

Select top 1 from DataTable where column order by Column as string

See the C# Datatable's values above. I need to get the highlighted value, based on colA, and dateof, which I could explain with SQL, like this:
SELECT TOP 1 colB FROM dt WHERE colA = 'aaa' ORDER BY dateof ASC
I have to do it in C#, not in SQL.
How can I get that value?
Can you please try the following code and let me know whether it helps or not?
Let myDataTable,be the DataTable that you are processing, assume that the type of field is string(change appropriately if needed)
myDataTable.AsEnumerable()
.Where(x => x.Field<string>("colA") == "aaa")
.OrderBy(y => y.Field<string>("dateof"))
.Take(1)
.Select(s => s.Field<string>("colB"))
Or else this may help you:
var defaultSelectedRow = myDataTable.AsEnumerable()
.Where(x => x.Field<string>("colA") == "aaa")
.OrderBy(y => y.Field<string>("dateof")).FirstOrDefault();
if (defaultSelectedRow != null)
{
string colBValue = defaultSelectedRow.Field<string>("colB");
}

LINQ: How to get the Max Id with a group by clause?

I am looking for a way in LINQ to get a max Id record by using 'Group By' clause
Consider the following Sample data
Table: ProcessAud
ProcessSeq ProjectSeq ProjectValue Active
11 1 50000 Y
12 1 10000 Y
13 2 70000 Y
14 2 90000 Y
In which I want to get two records as a list such that is second and fourth
records (i.e) ProcessSeq 12 and 14. And I tried it like following
var ProcessAudList = ProcessAudService.FilterBy(x => x.Active == "Y"
).GroupBy(x => x.ProjectSeq).Max().ToList();
It is not working properly, So how to do it in LINQ. Please anybody help.
You want to pick top record from each group.
var ProcessAudList = ProcessAudService.Where(x => x.Active == "Y")
.GroupBy(x => x.ProjectSeq, (key,g)=>g.OrderByDescending(e=>e.ProjectValue).First());
Check demo code
When you use GroupBy extension, method will return you IGrouping instance and you should query IGrouping instance like below;
var ProcessAudList = collection.Where(x => x.Active == "Y").GroupBy(x => x.ProjectSeq).Select(x => x.OrderByDescending(a => a.ProcessSeq).FirstOrDefault()).ToList();
Hope this helps
You're most of the way there, but Max is the wrong term to use.
Each IGrouping is an IEnumerable (or IQueryable) sequence of its own, so you can use OrderBy and First clauses to get the answer you need:
var ProcessAudList = ProcessAudService
.FilterBy(x => x.Active == "Y")
.GroupBy(x => x.ProjectSeq)
.Select(grp => grp.OrderByDescending(x => x.ProcessSeq).First())
.ToList();
The Select clause will process each of the groups, order the groups descending by ProcessSeq and select the first one. For the data you provided this will select the rows with ProcessSeq equal to 12 and 14.
With this code you can get all max id in foreach
var res = from pa in ProcessAud
group Cm by pa.ProjectSeq into Cm1
select new
{
_max = Cm1.Max(x => x.ProcessSeq)
};
foreach (var item in res)
{
//item._max have biggest id in group
}

Ordering not working in Entity Framework query

I'm currently battling a linq query for my application using Entity Framework (6.1.3)
The query is as follows:
var productPeriods = (from pp in ctx.ProductPeriods
where pp.IsActive && pp.Product.IsBuyBackForProduct == null && !pp.Product.ProductAddOns.Any() && pp.PowerRegionID == powerRegionId
select new
{
ProductPeriod = pp,
Price = pp.Prices
.OrderByDescending(x => x.Created)
.GroupBy(x => x.FirmID)
.Select(pr => pr.FirstOrDefault())
.OrderByDescending(x => x.ProductPrice)
.FirstOrDefault()
}).ToList();
The purpose of the query is to find the latest price from the prices collection of a product period, grouped by the firm ID and then select the best price of the latest prices from each firm.
This works perfectly in Linqpad, but the first OrderByDescending(x => x.Created) doesn't work when used in context of Entity Framework.
Does anyone knows why? And perhaps have a solution for it? :-)
Thanks in advance!
Update
Thanks for all replies. I've tried the following:
select new {
ProductPeriod = p,
Price = p.Prices.GroupBy(x => x.FirmID).Select(pr => pr.OrderByDescending(x => x.Created).ThenByDescending(x => x.ProductPrice).FirstOrDefault())
}
But it seems like ThenByDescending(x => x.ProductPrice) gets ignored as well. The prices are not sorted correctly in the output. They're output like this:
Price: 0,22940, Created: 06-03-2015 10:15:09,
Price: 0,23150, Created: 06-03-2015 10:05:48
Price: 0,20040, Created: 06-03-2015 09:24:24
Update 2 (solution for now)
I came to the solution that the initial query just returns the latest prices from each firm. There's currently three firms, so the performance should be alright.
Later in my code, where I'm actually using the latest and best price, I simply do an .OrderByDescending(x => x.ProductPrice).FirstOrDefault() and check if it's not null.
I.e:
var productPeriods = (from pp in ctx.ProductPeriods
where pp.IsActive && pp.Product.IsBuyBackForProduct == null && !pp.Product.ProductAddOns.Any() && pp.PowerRegionID == powerRegionId
select new
{
ProductPeriod = pp,
Prices = pp.Prices.GroupBy(x => x.FirmID).Select(pr => pr.OrderByDescending(x => x.Created).FirstOrDefault())
}).ToList();
Later in my code:
var bestPriceOfToday = period.Prices.OrderByDescending(x => x.ProductPrice).FirstOrDefault()
The problem is the commands you are using. OrderBy and OrderByDescending do NOT add additional order by statements to the resulting query but instead they CREATE the order by statement and eliminate all orderby statements that existed before.
In order to use multiple orderby's you need to do the following:
OrderBy or OrderByDescending
ThenBy or ThenByDescending
the ThenBy statements can be used 1 or more times they just add additional order statements to the resulting query.
According to yours update, omnit select and type:
select new {
ProductPeriod = p,
Price = p.Prices.GroupBy(x => x.FirmID)
.OrderByDescending(x =>x.Created).ThenByDescending(x=>x.ProductPrice).FirstOrDefault()
}
That select was useless and could be the cause of problem

Linq to SQL order by with Distinct

My Environment: ASP.net and C# in VS 2013 Express.
I have been through many similar SO articles trying to work this out. I am amateur with Linq to SQL queries and c# in general.
I'm trying to use Linq to SQL to get the top 5 most recent distinct values from a column, then add them to a list. My application is asp.net using c# and a .dbml file for data abstraction.
I've tried it many different ways. I either get non-distinct yet sorted list, or I get a distinct unsorted list. What I have so far is below
var Top5MFG = (from mfg in db.orders
where mfg.manufacturer.Length > 0 && mfg.customerid == "blahblahblahblahblah"<br />
select new {
manufacturer = mfg.manufacturer,
date = mfg.date_created
})
.Distinct()
.OrderByDescending(s => s.date);
I'm thinking my "Distinct" is looking at the "ID" column, and perhaps I need to tell it I want it to look at the "manufacturer" column, but I haven't worked out how / if it's possible to do that.
I could do this with ease by using a storedproc, but I'm really trying to do it with c# code directly if possible. This is my first post to SO, I hope I have put it together properly. Any help much appreciated.
Thanks
No the Distinct compares manufacturer and date pairs.If you want to get distinct records by manufacturer then I recommend DistinctBy method.It's in the MoreLINQ library.Since its a third library method it's not supported in linq to sql, you still can use it by fetching the records from DB and do the rest in memory
(from mfg in db.orders
where mfg.manufacturer.Length > 0 && mfg.customerid == "blahblahblahblahblah"
select new {
manufacturer = mfg.manufacturer,
date = mfg.date_created
})
.AsEnumerable()
.DistinctBy(x => x.manufacturer)
.OrderByDescending(s => s.date)
.Take(5);
I think you can use the GroupBy to do what you want.
var Top5MFG = db.orders
.Where (x => x.manufacturer.Length > 0 && x.customerid == "blahblahblahblahblah")
.GroupBy(mfg => mfg.manufacturer)
.Select(g => g.First())
.OrderByDescending(d => d.date_created );
.Take(5);
One way you can distinct by a certain field is to replace:
...
.Distinct()
...
with:
...
.GroupBy(x => x.manufacturer )
.Select(g => g.First())
...

Linq select with filtering not working

I'm trying to select one field last record in filtered database (this is different than last inserted record). I tried with following code in controller but instead of field value, i'm getting "true" or "false", depending on if there's results after filtering or not.
List<Pozicije> poz = new List<Pozicije>();
poz = db.Pozicijes.Where(p => p.grupa == grupa)
.OrderBy(p => p.sifra_pozicije).ToList();
string pos = poz.Select(p => p.sifra_pozicije.Contains(s)).LastOrDefault().ToString();
can someone point me how to get value i need instead?
Try this instead. I've combined both parts of your query into one.
var pos =
Convert.ToString(db.Pozicijes.Where(p => p.grupa == grupa
&& p.sifra_pozicije.Contains(s))
.OrderByDescending(p => p.sifra_pozicije)
.Select(p => p.sifra_pozicije)
.FirstOrDefault());
If it doesn't work, you may need to tell us what types s and sifra_pozicije are.
LastOrDefault is not supported with LINQ to Entities/LINQ TO SQL. You need to do OrderByDescending and then get First record. Like:
string pos = db.Pozicijes.Where(p => p.grupa == grupa && p.sifra_pozicije.Contains(s)))
.OrderByDescending(p=> p.sifra_pozicije)
.Select(r=> r.sifra_pozicije)
.First();

Categories