Given the following tables I'd like to return the localised text for given culture or the text for the default culture where no row exist for the given culture.
diagram http://lh4.ggpht.com/_gjsCWAV_CZc/ShW6hC-eozI/AAAAAAAACbY/mXaBfiZtBY8/s400/diagram.png
So with the folowing data
Resources
ID Name
1 Donkey
2 Elephant
LocaleStrings
ID CultureID ResID LocaleText
1 1 1 Donkey
2 1 2 Elephant
3 2 1 baudet
I'd like to be able to return the following for the French culture
baudet
elephant
I've tried various queries based around LEFT JOINS samples I've seen but I'm stuck.
var ct = from r in db.Resources
join lt in db.LocaleStrings
on r.ID equals lt.ResID into res
from x in res.DefaultIfEmpty()
select new
{
CultureID = x.CultureID,
LocaleText = x.LocaleText,
ResID = x.ResID
};
var text =
from c in db.Cultures
join t in ct
on c.ID equals t.CultureID into cults
from x in cults.DefaultIfEmpty()
select x;
I'm sure there's a better way, but this seems to work:
var ct =
from c in db.Cultures
from l in db.LocaleStrings
from r in db.Resources
where r.ID == l.ResID
select new
{
CultureID = c.ID,
LocaleText = l.CultureID == c.ID ? l.LocaleText : r.Name,
ResID = r.ID,
LSID = l.CultureID == c.ID ? l.ID : 0
};
var text =
from t in ct
where t.LSID != 0 || (t.LSID == 0 && !((from ac2 in ct
where ac2.LSID > 0 && ac2.CultureID == t.CultureID
select ac2.ResID).Contains(t.ResID)))
select new
{
CultureID = t.CultureID,
LocaleText = t.LocaleText,
ResID = t.ResID
};
Related
In my ASP.NET MVC application, I have created an HTML Table using the view model.
For this, I have written a query that shows only the data that ReOrderQty is !=0 and AvaQty is less than ReOrderQty.
List < RecognizedPartsViewModel > Reco = new List < RecognizedPartsViewModel > ();
var rData = (from i in db.InventoryMain
join p in db.PartCategory on i.PartCatogary equals p.Id
where i.ReOrderQty != 0 && i.AvaQty <= i.ReOrderQty && i.PartCatogary != 0
select new RecognizedPartsViewModel {
Id = i.Id,
PartNo = i.PartNo,
Description = i.PartDescription,
Model = i.PartModel,
AvaQty = i.AvaQty,
ReOrderQty = i.ReOrderQty,
PartCato = i.PartCatogary,
ABCD = i.A_B_C_D_Category
}).ToList();
So as so far, table data is showing according to the query.
There is another table where I store Orders according to the PartId. So I want to show that data on another column in the same HTML Table.
I can get those details by joining the join ord in db.OrderTable on i.Id equals ord.PartNo_Id
but when it does results only show the PartNumbers that only contains on the OrderTable only.
This is how I modified it as I mention.
List < RecognizedPartsViewModel > Reco = new List < RecognizedPartsViewModel > ();
var rData = (from i in db.InventoryMain
join p in db.PartCategory on i.PartCatogary equals p.Id
join ord in db.OrderTable on i.Id equals ord.PartNo_Id
where i.ReOrderQty != 0 && i.AvaQty <= i.ReOrderQty && i.PartCatogary != 0
select new RecognizedPartsViewModel {
Id = i.Id,
PartNo = i.PartNo,
Description = i.PartDescription,
Model = i.PartModel,
AvaQty = i.AvaQty,
ReOrderQty = i.ReOrderQty,
PartCato = i.PartCatogary,
ABCD = i.A_B_C_D_Category,
PastOrders = "Order Qty: " + ord.OrderQty
}).ToList();
So, when this does like I was said earlier not show every record, it shows only the record in the ordertable.
So how I can show those tables within the same view without losing my main requirement?
That would be
left outer join
var rData = (from i in db.InventoryMain
join p in db.PartCategory on i.PartCatogary equals p.Id
join ord in db.OrderTable on i.Id equals ord.PartNo_Id into leftjoin
from order in leftjoin.DefaultIfEmpty()
where i.ReOrderQty != 0 && i.AvaQty <= i.ReOrderQty && i.PartCatogary != 0
select new RecognizedPartsViewModel {
Id = i.Id,
PartNo = i.PartNo,
Description = i.PartDescription,
Model = i.PartModel,
AvaQty = i.AvaQty,
ReOrderQty = i.ReOrderQty,
PartCato = i.PartCatogary,
ABCD = i.A_B_C_D_Category,
PastOrders = "Order Qty: " + order?.OrderQty ?? string.Empty
}).ToList();
Reference: Perform left outer joins
im required to make a query to db to fetch data fro a highchart widget in our site here is the code I use currently,
var highChartsData =
(from a in db.roomdetails
join b in db.ApplySchedule on a.RoomId equals b.RoomID
where b.Status == true
select new HighlinePie
{
Title = a.RoomName,
Date = b.MDate,
Value = db.ApplySchedule.Where(x => x.RoomID == a.RoomId).GroupBy(x=>x.MDate).Count(),
}).ToList();
The problem with this approach is right now get the total count but what i need is the count based on date, for example if there was two entry on date 12/09/20201 and three entry on 14/09/20201 the data should be "Title,12/09/20201,2","Title,14/09/20201,3".
You have to use grouping:
var groupQuery =
from a in db.roomdetails
join b in db.ApplySchedule on a.RoomId equals b.RoomID
where b.Status == true
group b by new { a.RoomName, b.MDate } into g
select new HighlinePie
{
Title = g.Key.RoomName,
Date = g.Key.MDate,
Value = g.Count()
};
var highChartsData = groupQuery.ToList();
I am working with some SQL that has been provided and I am running into an issue where the count in the LINQ is different from the count in the SQL and would appreciate some help if possible.
The original SQL that was given is pretty poorly arranged and has a large number of nested queries. This being one of them. I have broken the query down into its main component and am now stitching it back together with LINQ.
The SQL that I am working with is here :
DECLARE #QSCollectionId UNIQUEIDENTIFIER = 'f52ec043-b360-4266-f95f-08d7c66074pe';
SELECT fieldid
Count(fieldid) AS fieldcount,
Sum(CASE
WHEN answer = [value] AND Answer IS NOT NULL THEN 1
ELSE 0
END) AS FieldAnswerMatchCount
FROM (SELECT DISTINCT FCI.fieldid,
answer,
QSA.qsrid,
FAG.fieldid AS dependentfield,
FAG.value
FROM forms.fieldconstraints FCI
INNER JOIN forms.sectionfieldmappings SFM
ON FCI.fieldid = SFM.fieldid
INNER JOIN forms.qssectionmappings QSSM
ON SFM.sectionid = QSSM.sectionid
INNER JOIN sessions.qsr
ON QSSM.qsid = qsr.qsid
--AND Qsr.QSCollectionId=#QSCollectionId
LEFT JOIN forms.answerguides FAG
ON
FCI.dependantanswerguideid = FAG.answerguideid
LEFT JOIN sessions.qsranswers QSA
ON FAG.fieldid = QSA.fieldid
AND Qsr.QsrId = QSA.qsrid
) AS
FieldConstr
GROUP BY fieldid
The LINQ
var id = Guid.Parse("f52ec043-b360-4266-f95f-08d7c66074be");
var firstResultPartThree = (from fci in FieldConstraints
join sfm in SectionFieldMappings on fci.FieldId equals sfm.FieldId
join qssm in QSSectionMappings on sfm.SectionId equals qssm.SectionId
join qsr in QSRs on new { qssm.QSId } equals new { qsr.QSId }
join ag in AnswerGuides on fci.DependantAnswerGuideId equals ag.AnswerGuideId into agResult
from agJoin in agResult.DefaultIfEmpty()
join qsrAnswers in QSRAnswers on new { agJoin.FieldId, qsr.QsrId } equals new {qsrAnswers.FieldId, qsrAnswers.QsrId} into qsrAnswersResult
from qsrAnswersJoin in qsrAnswersResult.DefaultIfEmpty()
//where qsr.QSCollectionId == id
select new {
FieldId = fci.FieldId,
Answer = qsrAnswersJoin.Answer,
QsrId = (Guid?)qsrAnswersJoin.QsrId,
DependentFieldId = agJoin.FieldId,
Value = agJoin.Value
}
);
firstResultPartThree.Dump();
var firstResultPartTwo = (from fr in firstResultPartThree
where fr.FieldId == Guid.Parse("98CA6B6F-4070-4CEB-E9F1-08D7C66278F9")
group fr by fr.FieldId into grp
select new {
FieldId = grp.Key,
Fieldcount = grp.Count(),
FieldAnswerMatchCount = grp.Count(x => (Guid?)grp.Key.Value != null)
}
);
The result of the LINQ in the example below gives me
FieldId | fieldcount | FieldAnswerMatchCount
98ca6b6f-4070-4ceb-e9f1-08d7c66278f9 | 4 | 4 |
The result of the sql for the same data is
98ca6b6f-4070-4ceb-e9f1-08d7c66278f9 | 3 | 2 |
The area that I am struggling with is
select new {
FieldId = grp.Key,
Fieldcount = grp.Count(),
FieldAnswerMatchCount = grp.Count(x => (Guid?)grp.Key.Value != null)
I understand that the count in the select is wrong, however I do not know how I need to correct it and would be grateful for some help.
i'm trying to do this on LINQ:
select p.ProductID, p.ArticleCode, SUM((case h.OperationType when 0 then 1 else -1 end) * h.[Count])
from Products p
inner join StockItems s on p.ArticleCode = s.ArticleCode
inner join StockHistorical h on s.ArticleID = h.ArticleID
where h.[Date] < '23/08/2013 11:30:00'
group by p.ProductID, p.ArticleCode
I have this:
var products = (from p in e.Products
join s in e.StockItems on p.ArticleCode equals s.ArticleCode
join h in e.StockHistoricals on s.ArticleID equals h.ArticleID
where h.Date < DateTime.Parse("23/08/2013 11:30:00")
group p by p.ProductID into StockResult
select new { });
Anyone know how can i do the
SUM((case h.OperationType when 0 then 1 else -1 end) * h.[Count])
with LINQ?
Thanks!
I forgot to say that the problem is the "group by", because i can't access the OperationType property in the StockResult group.
Solved! The key is the:
let combined = new
{
Product = p,
HistoryCount = (h.OperationType == 0 ? 1 : -1) * h.Count
}
...
let combined = new
{
Product = p,
HistoryCount = (h.OperationType == 0 ? 1 : -1) * h.Count
}
group combined by combined.Product.ProductID into StockResult
select new
{
ProductID = StockResult.Key,
Total = StockResult.Sum(x => x.HistoryCount)
}
It's not a direct translation, but I'd go with:
(h.Count(his => his.OperationType != 0)
- h.Count(his => his.OperationType == 0))
* h.Count()
It should be functionally equivalent, and seems to better represent what you're trying to do.
You are trying to do something like this?
I am not sure this is going to work.
select new
{
S=Sum(((h.OperationType)!=0?-1:1)*h.Count)
}
I have a situation where my query returns the following two rows:
UserName ID Designation RoleID
shd.1234 3 1 2
shd.1234 3 1 5
I am able to display these results in a Jqgrid using LINQ.
However I want to display it as under in a single row:(if RoleID is 5, display it in another column in the same row.)
UserName ID Designation RoleID AdditionalRoleID
shd.1234 3 1 2 5
My current query is something like this:
empDetails = (from u in ObjectContext.USERS
join ed in ObjectContext.USERS_EMPLOYEE_DETAILS on u.UserID equals ed.UserID
join r in ObjectContext.ROLES_FOR_USERS on u.UserID equals r.UserID
join ro in ObjectContext.ROLES on r.RoleID equals ro.RoleID
where (r.HospitalID == Context.CurrentUser.HIdentity.HospitalID)
where(r.RoleID!= 4)
select new Models.AdminModelSettings.EmployeeDetailsForGivenHospital
{
UserName = u.UserName,
EmployeeId = ed.ID,
EmployeeDesignation = ed.Designation,
RoleID = r.RoleID,
RoleName = r.RoleID == 1 || r.RoleID == 2 || r.RoleID == 3 ? ro.RoleName : null,
AdditionalRole = r.RoleID == 5 ? ro.RoleName : null
}).ToList();
I would please like to know how this can be done in SQL/LINQ.
UPDATE: I've improved the code so it will do all of the work in SQL instead of returning the results and then performing the grouping.
This should do the trick!
The key part is where we order the rows by RoleID and then group the rows by ID. It assumes that you only have a Role and an Additional Role, i.e. it won't select an AdditionalRole2 or AdditionalRole3, etc...
var empDetails = from u in ObjectContext.USERS
join ed in ObjectContext.USERS_EMPLOYEE_DETAILS on u.UserID equals ed.UserID
join r in ObjectContext.ROLES_FOR_USERS on u.UserID equals r.UserID
join ro in ObjectContext.ROLES on r.RoleID equals ro.RoleID
where (r.HospitalID == Context.CurrentUser.HIdentity.HospitalID)
where(r.RoleID!= 4)
select new Models.AdminModelSettings.EmployeeDetailsForGivenHospital
{
UserName = u.UserName,
EmployeeId = ed.ID,
EmployeeDesignation = ed.Designation,
RoleID = r.RoleID,
RoleName = r.RoleID == 1 || r.RoleID == 2 || r.RoleID == 3 ? ro.RoleName : null
});
var roleIDs = new List<int> { 1, 2, 3 };
//group our results and order the group by the role id
var temp = empDetails.GroupBy(row => row.ID).Select(g => new { First = g.FirstOrDefault(r => roleIDs.Contains(r.RoleID)), Last = g.FirstOrDefault(r => r.RoleID == 5) });
//select the data into the shape that we want
var query = temp.Select(result => new Models.AdminModelSettings.EmployeeDetailsForGivenHospital
{
UserName = (result.First ?? result.Last).UserName,
EmployeeId = (result.First ?? result.Last).ID,
EmployeeDesignation = (result.First ?? result.Last).Designation,
RoleID = (result.First == null) ? (int?)null : result.First.RoleID,
AdditionalRoleID = (result.Last == null) ? (int?)null : result.Last.RoleID
});