I have the following schema:
Table1
ID int
Table2
ID int
Table1ID int
Datetime datetime
Table3
ID int
Table2ID int
Name varchar(255)
All columns are not null. How do I write the following SQL query in LINQ using lambda expressions?
select Table1.*
from Table2
inner join (
select Table1ID, max(Datetime) as Datetime
from Table2
group by Table1ID
) a on Table2.Table1ID = a.Table1ID and Table2.Datetime = a.Datetime
inner join Table3 on Table2.ID = Table3.Table2ID
inner join Table1 on Table1.ID = Table2.Table1ID
where Name = 'me'
EDIT:
I am using LINQ to EF. I have tried
var myEntities = new MyEntities();
var a = myEntities.Table2.Select(x => new { x.Id, x.Datetime }).GroupBy(x => x.Id).Select(x => new { Id = x.Key, Datetime = x.Max(y => y.Datetime) });
var b = myEntities.Table2.Join(a.ToList(), x => new { Id = x.Table1Id, x.Datetime }, y => new { y.Id, y.Datetime }, (x, y) => x.Id);
return myEntities.Table3.Where(x => x.Name == "me" && b.Contains(x.Table2Id)).Select(x => x.Table2.Table1).ToList();
but it comes back with
System.NotSupportedException: Unable to create a constant value of type 'Anonymous type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
highlighting the last line above. The stack trace shows it is ToList() throwing this exception.
I figured it out; it was that
var b = myEntities.Table2.Join(a.ToList(),
should be
var b = myEntities.Table2.Join(a,
Also, the query should be
var myEntities = new MyEntities();
var a = myEntities.Table2.Select(x => new { x.Table1Id, x.Datetime }).GroupBy(x => x.Table1Id).Select(x => new { Table1Id = x.Key, Datetime = x.Max(y => y.Datetime) });
var b = myEntities.Table2.Join(a, x => new { x.Table1Id, x.Datetime }, y => new { y.Table1Id, y.Datetime }, (x, y) => x);
return b.Join(myEntities.Table3, x => x.Id, y => y.Table2Id, (x, y) => new { x.Table1, y.Name }).Where(x => x.Name == "me").Select(x => x.Table1).ToList();
Related
I have the following query:
var query = db.DepoAccounts
.Join(db.DepoAccountDetails,
t1 => new { t1.INTEG_REF_NO, t1.BASE_DATE },
t2 => new { t2.INTEG_REF_NO, t2.BASE_DATE },
(t1, t2) => new DataCustomerAccountGDWHModel
{
BranchNumber = t1.BR_NO,
BookedBalance = t2.DP_FACE_BAL_VAL_ADJ,
}
)).Where(ac => ac.BranchNumber == "500" &&
(ac.AccountNumber.Substring(0, 1) == "H" ||
ac.AccountNumber.Substring(0, 1) == "F"));
How do I add to the above to do a correlated subquery?
AND DEP2.EFECT_DT = (SELECT MAX(DEP3.EFECT_DT) FROM
R_DBLNK_US.DW_TRN_DEPO_ACC_DTL_D DEP3
WHERE DEP.INTEG_REF_NO = DEP3.INTEG_REF_NO
AND DEP.BASE_DATE = DEP3.BASE_DATE)
I have a result coming back from a LINQ statement that looks like this:
Id dataId dataVal
A 1 1000
A 2 2000
A 3 3000
A 3 3001
A 3 3002
What I'd like is to just get the 1st item (dataId = 3 and dataVal = 3000)
Here is my query that is generating the above result:
var myIds = myList
.Where(a => ListIds.Contains(a.dataId))
.Select(x=> new
{
Id = x.Id,
DataId = x.dataId,
DataValue = x.DataValue
}).ToList().Distinct();
Do I need to do some grouping or is there an easier way?
Group your items by dataId, and then select first item from each group:
var myIds = (from a in myList
where ListIds.Contains(a.dataId)
group a by a.dataId into g
let firstA = g.OrderBy(x => x.DataValue).First()
select new {
Id = firstA.Id,
DataId = g.Key,
DataValue = firstA.DataValue
}).ToList();
Or with extension methods (it returns first item in original order):
var myIds = myList
.Where(a => ListIds.Contains(a.dataId))
.GroupBy(a => a.dataId)
.Select(g => new
{
Id = g.First().Id,
DataId = g.Key,
DataValue = g.First().DataValue
}).ToList();
Use .FirstOrDefault() after the Select
var myIds = myList
.Where(a => ListIds.Contains(a.dataId))
.Select(x=> new
{
Id = x.Id,
DataId = x.dataId,
DataValue = x.DataValue
}).FirstOrDefault();
I tried a few different ways and I am not impressed with the generated output query. It seems so inefficient. It needs to be efficient and shouldn't bring back all the rows into the app layer
select year(datetaken) as yr,
month(datetaken) as mth,
day(datetaken) as dy,
count(*) as totalpics
from photos
where photos.dateTaken <= #cutoffdate
group by year(datetaken), month(datetaken), day(datetaken)
order by yr asc, mth asc, dy asc
LINQ query:
var query = ctx.Photos.Where(p => p.DateTaken <= maxCutOffDate)
.GroupBy(p => new { p.DateTaken.Year, p.DateTaken.Month, p.DateTaken.Day })
.Select(grp => grp);
var results = query.ToList();
Select only the grouping key (grp.Key):
var query = ctx.Photos.Where(p => p.DateTaken <= maxCutOffDate)
.GroupBy(p => new { p.DateTaken.Year, p.DateTaken.Month, p.DateTaken.Day })
.Select(grp => grp.Key);
var results = query.ToList();
Edit
Or, including the count of totalpics:
var query = ctx.Photos.Where(p => p.DateTaken <= maxCutOffDate)
.GroupBy(p => new { p.DateTaken.Year, p.DateTaken.Month, p.DateTaken.Day })
.Select(grp => new
{
Date = grp.Key,
TotalPics = grp.Count()
});
var results = query.ToList();
You can access the key properties through grp.Key and the count through grp.Count().
var query = ctx.Photos.Where(p => p.DateTaken <= maxCutOffDate)
.GroupBy(p => new { p.DateTaken.Year, p.DateTaken.Month, p.DateTaken.Day })
.Select(grp => new
{
grp.Key.Year,
grp.Key.Month,
grp.Key.Day,
Count = grp.Count()
});
var results = query.ToList();
Say I have the following:
var OrderCounts = from o in Orders
group o by o.CustomerID into g
select new {
CustomerID = g.Key,
TotalOrders = g.Count()
};
How can this be converted to a Lambda expression
var OrderCounts = customers
.GroupBy (o => o.CustomerID)
.Select (o => new { CustomerID = o.Key, TotalOrders = o.Count () })
I have a list of objects, those objects may or may not have contact info:
// Join contact
query = query.Join(
(new ContactRepository(this.Db)).List().Where(x => x.IsMainContact),
x => new { x.ListItem.ContentObject.LoginId },
y => new { y.LoginId },
(x, y) => new ListItemExtended<ListItemFirm>
{
City = y.City,
State = y.State,
Country = y.Country
});
This does inner join on 'LoginId'. But I need an outter join so that if contact info does not exists for a given LoginId it will be empty.
Please help
thanks
You should execute outer join manually:
var contacts = (new ContactRepository(this.Db)).List();
query.Select(item =>
{
var foundContact = contacts.FirstOrDefault(contact => contact.Id == item.Id);
return new ListItemExtended<ListItemFirm>()
{
Id = item.Id,
City = foundContact != null ? foundContact.City : null,
State = foundContact != null ? foundContact.State : null,
Country = foundContact != null ? foundContact.Country : null,
};
})
But remember that if your Contact item is struct - checking for null isn't proper way. Use Any() operator instead of FirstOrDefault().