How to solve System.OutofMemoryException in linq using C#? - c#

I have two list. First list consist 70000 row and second list consist 20000 row. After join two list 'System.OutofMemoryException' error occour.
Here my code:
var resultx12 = (from a in resultx
join b in resultx1
on new { stdtab = a.sectionStd.Trim(), stdli = a.liStd.Trim() }
equals new { stdtab = b.sectionStd.Trim(), stdli = b.liStd.Trim() }
into tempBrokerBogeyMapped
from x in tempBrokerBogeyMapped.DefaultIfEmpty()
.Skip(totalFetched)
.Take(1000)
select new LiReportCompareContainer
{
Origin = Origin,
Broker = data.Broker,
FormulaStd = a.FormulaStd,
Formula = x == null ? "" : String.IsNullOrEmpty(x.Formula) ? "" : x.Formula.Trim(),
Group = a.Group,
liBroker = a.liBroker,
Link = a.Link,
liStd = a.liStd,
Period1 = a.Period1,
Period1Value = a.Period1Value,
Period2 = a.Period2,
Period2Value = a.Period2Value,
Period3 = a.Period3,
Period3Value = a.Period3Value,
sectionBroker = a.sectionBroker,
sectionStd = a.sectionStd,
Unit = a.Unit,
}).ToList();
System.OutofMemoryException error showing.

Related

More efficient Linq expression

I have this Linq Join
var NewQuote = (from qw in Q
join NW in NewNotes on qw.RECID equals NW.RECID into temp
from j in temp
select new Quotes
{
QuoteNumber = qw.QuoteNumber,
CustPartNumber = qw.CustPartNumber,
ITEMGROUPID = qw.ITEMGROUPID,
LotSize = qw.LotSize,
EAU = qw.EAU,
CONTACTPERSONID = qw.CONTACTPERSONID,
QUOTATIONSTATUS = qw.QUOTATIONSTATUS,
QUOTESENTDATE = qw.QUOTESENTDATE,
PricePerPiece = qw.PricePerPiece,
QuoteValue = qw.QuoteValue,
Email = qw.Email,
RECID = qw.RECID,
Notes = j == null ? "" : j.NOTES
}).ToList();
Q is of the Quote class but I need to add the data to the Notes field from NewNotes. Is there a better way to do this than listing every field from the Quote class? If I have to add fields to Quote then I have to document to come to this section of code and update as well.
Why you create new instances of Quotes if you just want to update one property?
var query = from qw in Q join NW in NewNotes
on qw.RECID equals NW.RECID into temp
from j in temp
select new { Quote = qw, Notes = j?.Notes ?? "" };
foreach(var x in query)
{
x.Quote.Notes = x.Notes;
}

How to write two left joins in the linq

var CCEScholasticTests = db.CCEScholasticTests.Where(x => x.CCEvaluationID == CCEValuationID && x.SubjectID == SubjectID && x.ClassID == ClassID && (x.BranchSectionID == BranchSectionID || BranchSectionID == 0) && x.languageTypeSubjectID == languageTypeSubjectID && (x.BranchID == BranchID || x.BranchID == 0) && x.IsOnlyGrade == false).Select(x => x).ToList();
var res = (from a in CCEScholasticTests
join b in db2.CCESubjectSkills on new { key1 = a.CCESubjectSkillID } equals new { key1 = b.CCESubjectSkillID } into join1
from joinRes in join1.DefaultIfEmpty(new CCESubjectSkill())
join c in db2.CCEScholasticSkillsMasters on new { key = joinRes.CCEScholasticSkillMasterID } equals new { key = c.CCEScholasticSkillMasterID } into join2
from joinRes2 in join2.DefaultIfEmpty(new CCEScholasticSkillsMaster())
select new EvaluationBluk
{
BranchSectionID = a.BranchSectionID,
CCEScholasticTestID = a.CCEScholasticTestID,
CCEScholasticTestName = a.CCEScholasticTestName,
//CCESubjectSkillName = joinRes.CCESubjectSkillName,
CCESubjectSkillID = a.CCESubjectSkillID,
CCEvaluationID = a.CCEvaluationID,
MaxMarks = a.MaxMarks,
SubjectID = a.SubjectID,
TestDate = a.TestDate,
MarksEntryLastDate = a.MarksEntryLastDate,
//CCEScholasticSkillMasterID = joinRes.CCEScholasticSkillMasterID,
//CCEScholasticSkillName = joinRes2.CCEScholasticSkillName
}).ToList();
how to write multiple left joins in linq.. the below linq query i am writing based on this sp
CREATE procedure [dbo].[GetClassCCEScholasticTestsOnlyMarksTest]
(
#BranchSectionID int,
#CCEvaluationID int,
#SubjectID int,
#ClassID int ,
#languageTypeSubjectID int ,
#BranchID int
)
as
select c1.BranchSectionID,c1.CCEScholasticTestID,c1.CCEScholasticTestName,s1.CCESubjectSkillName,
c1.CCESubjectSkillID,c1.CCEvaluationID,c1.MaxMarks,c1.SubjectID,convert(varchar,c1.TestDate,101) as TestDate,convert(date,c1.MarksEntryLastDate,101)as MarksEntryLastDate
,isnull(s1.CCEScholasticSkillMasterID,-1) as CCEScholasticSkillMasterID ,cm.CCEScholasticSkillName
from dbo.CCEScholasticTests c1
left join CCESubjectSkills s1 on s1.CCESubjectSkillID=c1.CCESubjectSkillID
left join CCEScholasticSkillsMaster cm on cm.CCEScholasticSkillMasterID=s1.CCEScholasticSkillMasterID
where c1.CCEvaluationID=#CCEvaluationID and c1.SubjectID=#SubjectID
and c1.ClassID=#ClassID and (c1.BranchSectionID=#BranchSectionID or c1.BranchSectionID=0) and c1.languageTypeSubjectID =#languageTypeSubjectID
and (c1.BranchID=#BranchID or c1.BranchID=0)and c1.IsOnlyGrade=0
order by c1.CCEScholasticTestID asc
in sql it throws 3 rows but in linq it throws 1 row only... what wrong in my code
var result=CCEScholasticTests.join(db2.CCESubjectSkills ,o=>o.CCESubjectSkillID, od=>od.CCESubjectSkillID,(o, od)=> new
{
BranchSectionID = o.BranchSectionID,
CCEScholasticTestID = o.CCEScholasticTestID,
CCEScholasticTestName = o.CCEScholasticTestName,
CCESubjectSkillID = o.CCESubjectSkillID,
CCEvaluationID = o.CCEvaluationID,
MaxMarks = o.MaxMarks,
SubjectID = o.SubjectID,
TestDate = o.TestDate,
MarksEntryLastDate = o.MarksEntryLastDate,
CCEScholasticSkillMasterID = od.CCEScholasticSkillMasterID
}).join(db2.CCEScholasticSkillsMasters, s=>s.CCEScholasticSkillMasterID = t.CCEScholasticSkillMasterID, t=>t.CCEScholasticSkillMasterID,(s,t)=> new
{
BranchSectionID = o.BranchSectionID,
CCEScholasticTestID = o.CCEScholasticTestID,
CCEScholasticTestName = o.CCEScholasticTestName,
CCESubjectSkillID = o.CCESubjectSkillID,
CCEvaluationID = o.CCEvaluationID,
MaxMarks = o.MaxMarks,
SubjectID = o.SubjectID,
TestDate = o.TestDate,
MarksEntryLastDate = o.MarksEntryLastDate,
CCESubjectSkillName = t.CCESubjectSkillName,
CCEScholasticSkillMasterID = t.CCEScholasticSkillMasterID,
CCEScholasticSkillName = t.CCEScholasticSkillName
}).tolist();
I dont know the exact relation between the table but, i have assumed and written query. hope it helps u

Linq C#, less performance with join vs multiple lookups

I have an older version of code that was doing multiple lookup. I needed some additional fields so instead i rewrote it to do a couple joins and return everything i needed at once. I imagined this would have better performance, but when testing with a stopwatch this was not the case and quite slower. Am i inadvertently doing something wrong with my query?
Old Code
// ===============================old
var watchOld = System.Diagnostics.Stopwatch.StartNew();
var def = context.FDataMLBPlayers.Where(d => d.Status.Trim().ToLower().Equals("active") && d.Position.Trim().ToLower().Equals(position.ToLower())).OrderBy(d => d.Team);
foreach (var dd in def)
{
bool addPlayer = false;
foreach (var tm in teams)
{
if (tm.Trim().Equals(dd.Team.Trim()))
{
addPlayer = true;
break;
}
}
if (!addPlayer) continue;
Player player = new Player();
player.Name = dd.FirstName.Trim() + " " + dd.LastName.Trim();
player.LastName = dd.LastName.Trim();
player.Number = dd.Jersey.HasValue ? dd.Jersey.Value : 0;
player.PlayerID = dd.PlayerID;
player.Points = 0;
player.Position = (dd.Position == null) ? "" : dd.Position.Trim();
player.Score = 0;
player.Status = "Active";//DOROC dd.CurrentStatus;
player.Team = dd.Team.Trim();
htop ht = GetOpposingTeam(dd.Team.Trim()); //another lookup
player.OpposingTeam = ht.OpposingTeam;
player.PhotoUrl = dd.PhotoUrl.Trim();
player.IsHomeTeam = ht.IsHomeTeam;
if (dd.Position != null)
{
list.Add(player);
}
}
watchOld.Stop();
var oldTime = watchOld.ElapsedMilliseconds; // 227 ms
New Code
// ========================================== new
var watchNew = System.Diagnostics.Stopwatch.StartNew();
var def2 = (from p in context.FDataMLBPlayers
join g in context.FDataMLBPlayerGames on p.PlayerID equals g.PlayerID
join t in context.FDataMLBTeams on g.OpponentID equals t.TeamID
where p.Status.Trim().ToLower().Equals("active")
&& p.Position.Trim().ToLower().Equals(position.ToLower())
&& teams.Contains(p.Team)
group new { p, g, t }
by new { p.PlayerID } into pgt
let fod = pgt.FirstOrDefault()
select new Player
{
DateTime = fod.g.DateTime.Value,
IsHomeTeam = fod.g.HomeOrAway.ToLower().Equals("home") ? true : false,
LastName = fod.p.LastName,
Name = fod.p.FirstName.Trim() + " " + fod.p.LastName.Trim(),
Number = fod.p.Jersey.HasValue ? fod.p.Jersey.Value : 0,
OpposingTeam = fod.t.Name,
PhotoUrl = fod.p.PhotoUrl,
PlayerID = fod.p.PlayerID,
Points = 0,
Position = fod.p.Position,
Score = 0,
Started = fod.g.Started.Value.Equals(1) ? true : false,
Status = fod.p.Status,
Team = fod.p.Team,
}
).ToList();
list = def2;
watchNew.Stop();
var newTime = watchNew.ElapsedMilliseconds; // 399 ms

Using LINQ left join with multiple conditions and subquery

I want to left join two tables and sum one field so I made this query:
IQueryable<Reference.Inventory.SearchDetailRequester> _qRequester =
from a in dbErp.EPROC_TR_ER_DETAIL
join b in dbErp.EPROC_TR_INVENTORY on
new Reference.Inventory.SearchDetailRequester { ID_REQUEST = a.ID_REQUEST , ID_KATALOG = a.ID_KATALOG}
equals
new Reference.Inventory.SearchDetailRequester { ID_REQUEST = b.ID_REQUEST, ID_KATALOG = b.ID_KATALOG }
into inv_join
from c in inv_join.DefaultIfEmpty()
where a.ID_REQUEST == ID_REQUEST && a.APROVE_BY_DS == 1 && a.APROVE_BY_GS == 1
select new Reference.Inventory.SearchDetailRequester
{
ID_KATALOG = a.ID_KATALOG,
TYPE_OF_GGS = a.TYPE_OF_GGS,
TRANSACTION_TYPE = "OUT",
DATE = c.DATE ?? "",
QTY = -1 * c.QTY ?? a.QTY,
ID_INVENTORY = c.ID_INVENTORY,
QTY_AVAILABLE = ((from d in dbErp.EPROC_TR_INVENTORY
where d.ID_KATALOG == a.ID_KATALOG
group d by new { d.ID_KATALOG } into e
select new { qty_ava = (System.Int32)e.Sum(p => p.QTY ?? 0) }).FirstOrDefault().qty_ava)
};
but when I debug I got this message :
The type 'Reference.Inventory.SearchDetailRequester' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be initialized in two places in the same query, but only if the same properties are set in both places and those properties are set in the same order.
Is there anyone can help?
You need to to the join with anonymous type, I don't think you can choose a class like you did:
new { ID_REQUEST = a.ID_REQUEST , ID_KATALOG = a.ID_KATALOG}
equals
new { ID_REQUEST = b.ID_REQUEST, ID_KATALOG = b.ID_KATALOG }
If for example b.ID_KATALOG is nullable in the database, you can solve it like this:
new { ID_REQUEST = a.ID_REQUEST , ID_KATALOG = a.ID_KATALOG}
equals
new { ID_REQUEST = b.ID_REQUEST, ID_KATALOG = (int)b.ID_KATALOG }
That is assuming ID_KATALOG is an int of course.
Or you can do it the other way around too normally:
new { ID_REQUEST = a.ID_REQUEST , ID_KATALOG = (int?)a.ID_KATALOG}
equals
new { ID_REQUEST = b.ID_REQUEST, ID_KATALOG = b.ID_KATALOG }

Retrurn list of objects matching by property

I have a method for parsing XML:
public static List<Profile> Parse XML(string Document)
{
List<Profile> Result = new List<Profile>();
doc = XDocument.Load(Document);
Resoults = (from n in doc.Descendants("level")
select new Profile()
{
CurrentID = int.Parse(n.Attribute("CurrentID").Value),
Location = (from l in n.Element("ID").Elements("ID")
select new Location()
{
id = (int)(l.Attribute("id")),
x = (Single)l.Attribute("x"),
y = (Single)l.Attribute("y"),
z = (Single)l.Attribute("z")
}).ToList(),
Bank = (from l in doc.Descendants("Banker")
select new Banker()
{
BankID = (int)(l.Attribute("id")),
BankX = (Single)(l.Attribute("x")),
BankY = (Single)(l.Attribute("y")),
BankZ = (Single)(l.Attribute("z"))
}).ToList(),
Vendor = (from l in doc.Descendants("Vendor")
select new Vendor()
{
VendorID = (int)(l.Attribute("id")),
VendorX = (Single)(l.Attribute("x")),
VendorY = (Single)(l.Attribute("y")),
VendorZ = (Single)(l.Attribute("z"))
}).ToList()
}).ToList();
var ProperID = Resoults.Where(s => s.CurrentID <= 10).Aggregate((c, d) => c.CurrentID > d.CurrentID ? c : d);
return ProperID; //error: Here i want to return list ProperID
}
I want to parse XML file and then get node out of parsed list with specific CurrentID.
I want to return ProperID list but compiler errores out with:
Cannot implicitly convert type 'Classes.XMLprofile.Profile' to 'System.Collections.Generic.List<Classes.XMLprofile.Profile>'
You want return Results that have proper id in CurrentId,
In code you got compiler error because of return value is a Profile object and method signature is a List of Profile objects, So:
return Resoults.Where(p=>p.CurrentID ==ProperID.CurrentID).ToList();

Categories

Resources