How do i do the "IN" and "DISTINCT of SQL to LINQ - c#

SELECT DISTINCT MarketTemplateId AS MarketTemplateID
FROM Market
WHERE Market.FixtureId = ? And
Market.MarketTemplateId In
('MW3W', '1HTG', 'FTCS', 'HTFT', 'MTG2W', 'MW3W1H','FTCSALT')
how would i produce this in LINQ because i tried the below code and it doesn't work
var result6 = entityTrading.Markets
.Where(p => p.FixtureId == InternalFixtureID_F.ToString())
.FirstOrDefault(p => MarketTemplateIds.Contains(p.MarketTemplateId));

List<string> MarketTemplateIds = new List<string>{ "MW3W", "1HTG", "FTCS", "HTFT", "MTG2W", "MW3W1H","FTCSALT" };
var result6 = entityTrading.Markets
.Where(p => p.FixtureId == InternalFixtureID_F.ToString()
and MarketTemplateIds.Contains(p.MarketTemplateId ))
.Select( u => u.MaketTemplateId).Distinct();

How about
var id = InternalFixtureID_F.ToString();
var result6 = entityTrading.Markets
.Where(p => p.FixtureId == id && MarketTemplateIds.Contains(p.MarketTemplateId))
.Select(m => m.MarketTemplateId)
.Distinct();

Related

convert SQL with subquery to LINQ (lamda expression)

Need help with converting this sql query
SELECT c.studentId, c.Name
FROM Classes c
WHERE c.Class = 'Math'
AND c.Grade IN 'yoklama', '2')
AND c.studentId IN (
SELECT c2.studentId
FROM Classes c2
WHERE c2.Class = 'Eng'
AND c.Grade IN ('yoklama', '2'))
I was trying like this but these keeps giving error that I am doing this wrong
var reportDtos = context.Classes.Where(c => pt.Class == 'Math' && c.Grade.HasValue
&& c.Grade == '2' || c.Grade == 'yoklama' && c.studentId.Contains(context.ParticipTests
.Where(x => x.Class == 'Math' &&x.Grade.HasValue && x.Grade == '2' || x.Grade == 'yoklama')
.ToList()
.Select(x => ))
.Include("Area")
.Include("Questions")
.Include("Class.Test")
.ToList()
.Select(pt => new ReportDto
{
...
}
First off, strings must always be double quoted " and not single quoted ' like you have some.
If you write some code by splitting up the steps it makes it more understandable.
var validGrades = new List<string> { "Yoklama", "2" };
var studentsInEnglish = context.ParticipTests
.Where(x => x.Class.Equals("Eng") && validGrades.Contains(x.Grade))
.Select(x => x.studentId);
var studentsInMath = context.Classes
.Where(x => x.Class.Equals("Math") && validGrades.Contains(x.Grade) && studentsInEnglish.Contains(x.studentId))
.Select(x => (x.studentId, x.Name));
Now with this, (if you really still want to) you can write a single query by plugging in and replacing.
var reportDtos = context.ParticipTests
.Where(
x => x.Class.Equals("Math") &&
new List<string> { "Yoklama", "2" }.Contains(x.Grade) &&
context.Classes
.Where(
y => y.Class.Equals("Eng") &&
new List<string> { "Yoklama", "2" }.Contains(y.Grade))
.Select(y => y.studentId)
.Contains(x.studentId))
.Select(x => (x.studentId, x.Name))
.ToList();
...But I like the first way better.

c# Lambda referencing class object in Select method

I've written this code which works:
var uniqueCustomerIdList = services
.GroupBy(x => x.CustomerId)
.Select(cl => new Customer
{
CustomerId = cl.First().CustomerId,
CustomerName = cl.First().CompanyName,
PdfServices = services.Where(x => x.CustomerId == cl.First().CustomerId).ToList(),
PdfServiceLines = services.Where(x => x.CustomerId == cl.First().CustomerId).ToList()
.GroupBy(l => l.ServiceDescription)
.Select(cy => new PdfServiceLine
{
ServiceName = cy.First().ServiceDescription,
Quantity = cy.Count(),
UnitPrice = cy.First().PlanCharge,
ServiceCharges = services.Where(x => x.CustomerId == cl.First().CustomerId && x.ServiceDescription == cy.First().ServiceDescription).Sum(y => y.TotalBill),
UsageCharges = usage.Where(x => x.CustomerId == cl.First().CustomerId && x.ServiceDescription == cy.First().ServiceDescription).Sum(y => y.Charge),
Total = cy.Sum(c => c.PlanCharge),
}).ToList(),
PdfUsages = usage.Where(x => x.CustomerId == cl.First().CustomerId).ToList()
})
.ToList();
I wanted to know if it's possible to reference values from the outer Select statement in the inner statement? As it looks rather clunky at the moment.
For instance in the outer Customer select I use PdfServices - can I use that in the inner select where I have ServiceCharges?
ServiceCharges = PdfServices.Where(s => s.ServiceDescription == cy.First().ServiceDescription).Sum(y => y.TotalBill)
instead of
ServiceCharges = services.Where(x => x.CustomerId == cl.First().CustomerId && x.ServiceDescription == cy.First().ServiceDescription).Sum(y => y.TotalBill),
Thanks,
Lee.
The results that you want to re-use are members of an anonymous object, not variables. As such, you cannot expect them to be available, like a variable would be, to the inner lambda. If you re-wrote your statement, you could store them in intermediate variables:
var uniqueCustomerIdList = services
.GroupBy(x => x.CustomerId)
.Select(cl =>
{
var pdfServices = services.Where(x => x.CustomerId == cl.First().CustomerId).ToList();
return new Customer
{
CustomerId = cl.First().CustomerId,
CustomerName = cl.First().CompanyName,
PdfServices = pdfServices,
PdfServiceLines = pdfServices
.GroupBy(l => l.ServiceDescription)
.Select(cy => new PdfServiceLine
{
ServiceName = cy.First().ServiceDescription,
Quantity = cy.Count(),
UnitPrice = cy.First().PlanCharge,
ServiceCharges = services.Where(x => x.CustomerId == cl.First().CustomerId && x.ServiceDescription == cy.First().ServiceDescription).Sum(y => y.TotalBill),
UsageCharges = usage.Where(x => x.CustomerId == cl.First().CustomerId && x.ServiceDescription == cy.First().ServiceDescription).Sum(y => y.Charge),
Total = cy.Sum(c => c.PlanCharge),
}).ToList(),
PdfUsages = usage.Where(x => x.CustomerId == cl.First().CustomerId).ToList()
};
})
.ToList();
Note that the lamda body now uses curly braces and a return statement. This is not convertible to an expression tree, and some ORM frameworks, like Entity Framework, will not be able to translate the lamda into SQL.

Lambda query previous parameter access

Is possible to access a parameter variable of a previous condition?
How to do this?
var result = collectionA
.First(a => a.id == 1)
.CollectionB
.SelectMany(b => b.CollectionC)
.Select(c => new { propA = a.id, propC = c.id });
You can write
var result = collectionA
.Where(q => q.id == 1).Take(1) //it will be collection of 1 element
.Select(a => a
.CollectionB
.SelectMany(b => b.CollectionC)
.Select(c => new { propA = a.id, propC = c.id })).First();
or
(new [] { collectionA.First(q => q.id == 1) })
.Select(a => a
....
or just
var a = collectionA.First(q => q.id = 1);
a.Select( .....
The solution based on #Artem's answer:
var result = CollectionA
.Where(a => a.id == 1)
.SelectMany(b =>
b.CollectionB
.SelectMany(c => c.CollectionC)
.Select(c => new { b.id, c.id })
);

How to optimize LINQ-2-SQL expression?

I have a fairly complicated query that would read from a table, then do group on CONTACT_ID, then select only those group with count of 1.
This query is fairly complicated and I have no idea how to optimize it in LINQ.
var linkTable = this.DB.Links
.Where(l=>l.INSTANCE_ID==123456 && l.CONTACT_ID.HasValue && l.ORGANISATION_ID.HasValue)
.Select(l => new { l.DEFAULT_LINKED_ORGANISATION, l.LINK_ID, l.CONTACT_ID });
var defaultOrganizationLinkQuery = linkTable
.Where(l => l.DEFAULT_LINKED_ORGANISATION)
.Select(l => l.LINK_ID);
var singleOrganizationLinkQuery = linkTable
.GroupBy(l => l.CONTACT_ID)
.Select(group => new
{
CONTACT_ID = group.Key,
contact_link_count = group.Count(),
LINK_ID = group.First().LINK_ID
})
.Where(l => l.contact_link_count == 1)
.Select(l => l.LINK_ID);
var merged = singleOrganizationLinkQuery.Union(defaultOrganizationLinkQuery);
I made shorter version, but I do not expect it to be faster. If it works and is not slower I would be satisfied:
var merged = this.DB.Links
.Where(l=>l.INSTANCE_ID==123456 && l.CONTACT_ID.HasValue && l.ORGANISATION_ID.HasValue)
.GroupBy(l => l.CONTACT_ID)
.SelectMany(s => s.Where(x => s.Count() == 1 || x.DEFAULT_LINKED_ORGANISATION)
.Select(link => link.LINK_ID));

Linq Group By & Sum Query On Multiple Columns

var fpslist = db.FPSinformations.Where(x => x.Godown_Code != null && x.Godown_Code == godownid).ToList();
var data1 = fpslist.GroupBy(x => x.Ration_Card_Type1)
.Select(x => new
{
CardType_Name = x.Key,
CardType_Count = x.Sum(y => y.Ration_Card_Count1)
}).ToList();
var data2 = fpslist.GroupBy(x => x.Ration_Card_Type2)
.Select(x => new
{
CardType_Name = x.Key,
CardType_Count = x.Sum(y => y.Ration_Card_Count2)
}).ToList();
var data3 = fpslist.GroupBy(x => x.Ration_Card_Type3)
.Select(x => new
{
CardType_Name = x.Key,
CardType_Count = x.Sum(y => y.Ration_Card_Count3)
}).ToList();
var data4 = fpslist.GroupBy(x => x.Ration_Card_Type4)
.Select(x => new
{
CardType_Name = x.Key,
CardType_Count = x.Sum(y => y.Ration_Card_Count4)
}).ToList();
var data5 = fpslist.GroupBy(x => x.Ration_Card_Type5)
.Select(x => new
{
CardType_Name = x.Key,
CardType_Count = x.Sum(y => y.Ration_Card_Count5)
}).ToList();
var GodownRCCount = data1.Where(x => x.CardType_Name != null).ToList();
var GodownRCCounts = GodownRCCount;
GodownRCCount = data2.Where(x => x.CardType_Name != null).ToList();
GodownRCCounts.AddRange(GodownRCCount);
GodownRCCount = data3.Where(x => x.CardType_Name != null).ToList();
GodownRCCounts.AddRange(GodownRCCount);
GodownRCCount = data4.Where(x => x.CardType_Name != null).ToList();
GodownRCCounts.AddRange(GodownRCCount);
GodownRCCount = data5.Where(x => x.CardType_Name != null).ToList();
GodownRCCounts.AddRange(GodownRCCount);
I have 10 Columns in My Database Like
Ration_Card_Type1
Ration_card_count1
Ration_Card_Type2
Ration_card_count2
Ration_Card_Type3
Ration_card_count3
Ration_Card_Type4
Ration_card_count4
Ration_Card_Type5
Ration_card_count5
Now What I want is to get the sum of Ration_Card_Counts and Its Type from its Type
Expected Output :
CardType_Name
CardType_Count
Well the above code works fine but I want to optimize it in max possible way as this will be inside a Loop and there are about 1.5 million records.
Thanks
Union should operate faster than AddRange!
you can try the following:
var data = (from g in fpslist.GroupBy(x => x.Ration_Card_Type1).Select(x => new
{
CardType_Name = x.Key,
CardType_Count = x.Sum(y => y.Ration_Card_Count1)
}).Union(
fpslist.GroupBy(x => x.Ration_Card_Type2).Select(x => new
{
CardType_Name = x.Key,
CardType_Count = x.Sum(y => y.Ration_Card_Count2)
})).Union(
fpslist.GroupBy(x => x.Ration_Card_Type3).Select(x => new
{
CardType_Name = x.Key,
CardType_Count = x.Sum(y => y.Ration_Card_Count3)
})).Union(
fpslist.GroupBy(x => x.Ration_Card_Type4).Select(x => new
{
CardType_Name = x.Key,
CardType_Count = x.Sum(y => y.Ration_Card_Count4)
})).Union(
fpslist.GroupBy(x => x.Ration_Card_Type5).Select(x => new
{
CardType_Name = x.Key,
CardType_Count = x.Sum(y => y.Ration_Card_Count5)
}))
select g).ToList();
In general from grouping I would go with Thomas!
Database grouping is far better for me, since you are fetching the data required aggregated and therefore the data transferred over the network if much less!
You can rewrite the same query using SQL and put some indexes(performance) :
SELECT Ration_Card_Type = Ration_Card_Type1, Ration_Card_Count = sum(Ration_card_count1)
FROM
FPSinformations
GROUP BY
Ration_Card_Type1
UNION
SELECT Ration_Card_Type = Ration_Card_Type2, Ration_Card_Count = sum(Ration_card_count2)
FROM
FPSinformations
GROUP BY
Ration_Card_Type2
UNION
SELECT Ration_Card_Type = Ration_Card_Type3, Ration_Card_Count = sum(Ration_card_count3)
FROM
FPSinformations
GROUP BY
Ration_Card_Type3
UNION
SELECT Ration_Card_Type = Ration_Card_Type4, Ration_Card_Count = sum(Ration_card_count4)
FROM
FPSinformations
GROUP BY
Ration_Card_Type4
UNION
SELECT Ration_Card_Type = Ration_Card_Type5, Ration_Card_Count = sum(Ration_card_count5)
FROM
FPSinformations
GROUP BY
Ration_Card_Type5
I am not sure but this query make me think about UNPIVOT maybe you can investigate in this direction too.

Categories

Resources