LINQ to DataSet, distinct by multiple columns - linq

Just wanted to check if there is way to do distinct by multiple columns. Thanks in advance!!!
BTW, I found a great LINQ extension here but need some guidance to use it for multiple columns

Well, you can do the projection first:
var qry = db.Customers.Select(cust => new {cust.ID, cust.Name, cust.Region})
.Distinct();
Or in query syntax:
var qry = (from cust in db.Customers
select new {cust.ID, cust.Name, cust.Region}).Distinct();
That do?

Instead of Distinct you can use Groupby and then selecting the Top Most record of each group
How to LINQ Distinct by Multiple Fields without anonymous types
return from o in objEntity
group o by new
{
o.Field1,
o.Field2,
o.Field3,
o.Field4,
o.Field5
} into grp
select grp.FirstOrDefault();
This will give you the EntityObject Rather than the AnonymousType

By "distinct by multiple columns" what you really mean is a group by.
When you ask for distinct, it means that you are getting ALL the distinct rows, or, a group by using all the columns in the table.
If you want to only get distinct groupings for a subset of the columns, then use a group by in your clause, specifying the columns to group by. Then, select the groups, as you only want one set of keys for each group.

Another easy option is to create a single distinct string.
var result = collection.DistinctBy(c => c.Field1 + "." + c.Field2 + "." + c.Field3);

var qry = (from cust in db.Customers
select new {cust.ID, cust.Name, cust.Region}).GroupBy(x => new { x.Name,x.Region}).select(z => z.OrderBy(i => i.cust).FirstOrDefault()).ToList();

Related

Linq query, return distinct on single field & returning subset of data

I have a Linq query that returns three data elements.
var billingDateResults = from s in Subscriptions
.Where(s => (s.ProductCode.Contains("myCode")))
select { g.ID, BillingDate =s.BILL_THRU, s.ProductCode};
I would like to do distinct type of query on this to limit the results to one record per ID.
var billingDateResults = from s in Subscriptions
.Where(s => (s.ProductCode.Contains("myCode")))
group s by s.ID into g
select g.FirstOrDefault();
This works but now returns all of the fields in the records and I would like to minimize the amount of data by limiting the results to only the 3 fields in the first example.
What is a good way to do this?
TIA
Group by those three fields then.
var billingDateResults =
from s in Subscriptions
where s.ProductCode.Contains("myCode")
group new
{
g.ID,
BillingDate = s.BILL_THRU,
s.ProductCode
} by s.ID into g
select g.First(); // FirstOrDefault is not necessary, the groups will be non-empty

Linq: Orderby when including multiple tables

Currently learning Linq to Entity. I been successful, but came stumped with the orderby clause and its use with multiple tables.
var query = from k in contxt.pages.Include("keywords")
where k.ID == vals.pageId select k;
My understanding with the code above is it creates an inner join where ID is equal to pageId.
So what I am having a difficult time visualizing is how I would perform an orderby on both tables?
I would like to sort on both tables.
I have tried:
var query = from k in contxt.pages.Include("keywords") where k.ID == vals.pageId orderby k.keywords.**?** select k;
The question mark is not supposed to be there. I am showing that the column that I would like to sort by isn't there. Trying this k.Kegwords. doesn't show the column.
I would write a SQL query as follows:
string query = "SELECT pages.page, pages.title, pages.descp, keywords.keyword
FROM pages INNER JOIN keywords ON pages.ID = keywords.pageID
ORDER BY keywords.sort, pages.page";
pages and keywords have a 1 to many relationship, which FK keywords.
Thank you,
deDogs
Here you go.
var result = (from x in pages
join y in keywords on x.ID equals y.pageID
orderby y.sort, x.page
select new
{
x.Page,
x.title,
x.descp,
y.keyword
});

LINQ self join in ASP.NET MVC3

I have a situation where I need to do a self join on a table in LINQ. The table consists of fields ItemsID, Title, SeriesTitle, and many other. An item can be either a series or members and I can tell that by looking into ItemId which has "S" or "M" letters on it. I need to retrieve all records that are member of a series with ItemId "S117". I can do this in simple SQL by the code below,
select i.Series_Title, i.Item_ID, i2.Item_ID as Member_ID,
i2.Title as Member_Title, i2.Series_Title as Member_Series_Title
from Items i join Items i2 on i.Series_Title = i2.Series_Title
where i.Item_ID = "S117"
Now, I translated this query in LINQ which goes as
items = _dataContext.Items.AsQueryable();
items = from series in items
join members in items on series.Series_Title.ToLower()
equals members.Series_Title.ToLower()
where series.Item_ID.ToLower().Equals(itemId)
select series;
The last line of this query select series will only retrieve series but not members and I need members also.
I am using MVC3 Razor view where I have to display almost all fields so I am not using select new {....}
Even when I tried to use select new {series, members}, I got this exception -
Cannot implicitly convert type
'System.Linq.IQueryable'
to 'System.LinQ.IQueryable<My.App.models.Items>'
An explicit conversion exist.
Any suggestions would be highly appreciated.
Try this:
var items1 = _dataContext.Items.AsQueryable();
var items2 = from series in items1
join members in items1 on series.Series_Title
equals members.Series_Title
where series.Item_ID== 'S117'
select series;

LINQ: Inner join to the First row in a sub query?

I have two classes, basically one holds Members and the other Sessions.
They are joined together with a common field called "name". There is one member but can be many Sessions.
So if I do a standard join I get back 1 member and many sessions. I just want to get back the first row of sessions.
The session has field called SessioEndTime. So I need to order by DESC on this to pick out the first record.
This is my linq; I have returns too many. I think I need a subquery but I am a little confused.
var sessions = from m in this.members
join s in this.sessions
on m.Name equals s.Name
select new { MemberName = m.Name, SessionTime = s.SessioEndTime};
Edit
To make it clear, imagine I have five members, each member has NUMEROUS sessions. I just wish to receive my five members but with only one session each, that session is the LATEST session which can be got from the SessioEndTime.
Try this:
var sessions =
from m in this.members
join s in
(
from se in this.sessions
group se by se.Name into g
select new {Name = g.Key, SessioEndTime = g.Max(a=>a.SessioEndTime)}
)
on m.Name equals s.Name
select new { MemberName = m.Name, SessionTime = s.SessioEndTime}

LINQ - join , group by (multiple fields), group by, select new some fields

i need select some fields from a table in the query using join, but in the select new statement i dont have acess from the fields of the join table.
var query = from p in persistencia.RequisicaoCompraItems
join s in persistencia.Suprimentos on p.SuprimentoID equals s.SuprimentoID
(i need get fields from this join)
group p by new {p.SuprimentoID, p.RequisicaoCompraItemID, p.RequisicaoCompraID } into x
from res in x
orderby x.Key.SuprimentoID
select new {res.SuprimentoID ,
res.RequisicaoCompraItemID,
**but in here i cant acess** };
Cheers**
They're in res.Key.SuprimentoID etc

Resources