Combining Sub Queries Into 1 Query Linq - linq

Is there a way I can rewrite the following query to make it just one query?
try
{
var fileIds = (from f in context.SignalTo
where f.SignalFileID == 2
select new { f.GFileID }).ToList();
foreach (var id in fileIds)
{
var pp = (from p in context.ProjectFiles
where p.FileID == id.GFileID && p.ProjectID == ProjectID
select p);
if (pp != null)
{
ProjectFiles projectFile =(ProjectFiles) pp;
projectFile.MStatus = Status;
projectFile.DateLastUpdated = DateTime.Now;
context.SaveChanges();
}
}
}

You can combine the two query parts of your code into one.
You would then need to loop over the result set, making your updates. You would then call context.SaveChanges to submit all changes in one batch.
I can't tell if your existing code actually runs or compiles, but you need something like this:
Get the list of file ids you're interested in:
var fileIds = from f in context.SignalTo
where f.SignalFileID == 2
select f.GFileID;
fileIds is at this point an IQueryable where I assume T is an Int. No query has been excuted yet.
Now you can do
var pps = from p in context.ProjectFiles
where fileIds.Contains(p.FileID) && p.ProjectID == ProjectID
select p;
Still no query executed.
Then iterate over the result set
foreach( var pp in pps ) // query executed now
{
pp.MStatus = Status;
pp.DateLastUpdated = DateTime.Now;
}
context.SaveChanges(); // batch of updates executed now

Related

EF Core performance issue when using dynamic query using Expressions in .Net

Requirement : Select list of records from table for members whose ID and First name matches with records.
The number of members in the request may vary as per the request:
I have written following code to generate dynamic query using expressions.
private Expression<Func<TmpOncMemberAuth, bool>> CreateQueryExpression(List<MemberRequest> memberRequests)
{
var type = typeof(TmpOncMemberAuth);
// member =>
var memberParam = Expression.Parameter(type, "member");
var requests = memberRequests;
Expression selectLeft = null;
Expression filterExpression = null;
foreach (var member in requests)
{
var comparison = BuildSubQuery(member, memberParam, type);
if (selectLeft == null)
{
selectLeft = comparison;
filterExpression = selectLeft;
continue;
}
filterExpression =
Expression.Or(filterExpression, comparison);
}
return Expression.Lambda<Func<TmpOncMemberAuth, bool>>
(filterExpression, memberParam);
}
private Expression BuildSubQuery(MemberRequest member, ParameterExpression memberParam, Type type)
{
// LHS
// member.MemberID
Expression leftMemberID = Expression.Property(memberParam, type.GetProperty("MemberId"));
Expression leftMemberFirstName = Expression.Property(memberParam, type.GetProperty("MemberFirstName"));
// RHS
Expression requestMemberID = Expression.Constant(member.MemberID);
Expression requestFirstName = Expression.Constant(member.FirstName);
// member.MemberID == requestMemberID
Expression compareID =
Expression.Equal(leftMemberID, requestMemberID);
Expression compareFName =
Expression.Equal(leftMemberFirstName, requestFirstName);
// condition for a member
Expression comparison = Expression.AndAlso(compareID, compareFName);
return comparison;
}
// query call in the main
var whereQuery = CreateQueryExpression(memberData);
var memberAuthData = await FindAllAsyncFromTemp(whereQuery);
This generate a SQL query in the below format, which take a lot time
SELECT [#].[CaseID] AS [CaseId], [#].[MemberID] AS [MemberId], [#].[MemberFirstName], [#].[MemberLastName], [#].[MemberDOB]
FROM [#ONCMemberAuth] AS [#]
WHERE ((CASE
WHEN (([#].[MemberID] = '12345') AND ([#].[MemberFirstName] = 'James') ) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END | CASE
WHEN (([#].[MemberID] = '6789') AND ([#].[MemberFirstName] = 'WILLERS') ) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END)
) = CAST(1 AS bit)
I need to create SQL query like below which executes faster than the above one
SELECT [#].[CaseID] AS [CaseId], [#].[MemberID] AS [MemberId], [#].[MemberFirstName], [#].[MemberLastName], [#].[MemberDOB]
FROM [#ONCMemberAuth] AS [#]
WHERE ((([#].[MemberID] = '1234') AND ([#].[MemberFirstName] = 'James')) OR (([#].[MemberID] = '56789') AND ([#].[MemberFirstName] = 'WILLERS') ) )

"invalidcastexception specified cast is not valid linq" exeception in LINQ query

var Player = from PSI in regConfig.Player_SeasonalInfos
from PPI in regConfig.Player_PermanentInfos
where PSI.PaymentId == PlayerPayment.PaymentId
&& PPI.PlayerId == PSI.PlayerId
select new {
PlayerIds = string.Join(",", PSI.PlayerId),
PlayerSeasonalId = PSI.PlayerSeasonalId,
CityId = PPI.CityId
};
foreach (var item in Player)
{
Player_SeasonalInfo PlayerSeasonalInfos =
(from PSI in regConfig.Player_SeasonalInfos
where PSI.PlayerSeasonalId ==item.PlayerSeasonalId
select PSI).FirstOrDefault();
PlayerSeasonalInfos.StatusId = item.CityId == 1 ? 1 : 2;
regConfig.SubmitChanges();
}
i have write this code but i am getting excepiton"invalidcastexception specified cast is not valid linq" on line"
from PSI in regConfig.Player_SeasonalInfos
where PSI.PlayerSeasonalId ==item.PlayerSeasonalId
select PSI"
please suggest.

Where clause using Expression tree builder

I have to fetch data from database and filter data using the linq where clause.
My filter is an integer column and it contains value more than 1000.
What i am doing in the code is, breaking this huge array into chunk of 1000's of each and putting it in the where clause of a base query
int j = 0;
int batchsize = 1000;
while ((j * batchsize) < items.Count())
{
List<long> batch = items.Skip(j * batchsize)
.Take(batchsize).ToList();
prequery = prequery.Where(x => batch.Contains(x.Id));
j++;
}
the query which is getting generated in sql is below,
SELECT
x.name,
x.email
FROM
table x
WHERE
x.Id IN (1,2,3,...,1000) AND
x.Id IN (1001,1002,1003....,2000)
i want the query to be generated as below,
SELECT
x.name,
x.email
FROM
table x
WHERE
x.Id IN (1,2,3,...,1000) OR
x.Id IN (1001,1002,1003....,2000)
can i achieve this using expression tree builder and generate the query dynamically, if so please help in doing
you could use the "Concat" API:
int j = 0;
int batchsize = 1000;
IQueryable<YourType> finalQuery = null;
while ((j * batchsize) < items.Count())
{
List<long> batch = items.Skip(j * batchsize)
.Take(batchsize).ToList();
if (finalQuery == null) {
finalQuery = prequery.Where(x => batch.Contains(x.Id));
}
else
finalQuery = finalQuery.Concat (prequery.Where(x => batch.Contains(x.Id)));
j++;
}
This will logically get you what you want: basically you want an "OR" operation between batches. Concat is translated into an "UNION ALL" database call.
BUT ... I don't understand why are you doing this, after all you are grabbing all your data, chunks are not helping you because in the end there will be only one statement executed.

Linq syntax join and group

Hello I need to join two tables (MainTransaction and Subtransaction), the problem here is I also want to get all the record of Maintransaction that's not in the Subtransaction, I am stuck in this part, how can I achieve this ?
protected object SelectMainTbl()
{
var mainIdAndSum = from st in t.subtransaction
group st by st.MainTransactionId into g
select new
{
Sum = (from r in g
select r.Amount).Sum(),
MainId = g.Key
};
var mainTbl = from main in t.maintransaction
join sub in mainIdAndSum on main.MainTransactionId equals sub.MainId
where main.IsEnabled == true && (sub.Sum - main.Amount != 0)
select main;
return mainTbl;
}
I think this is the query that you want:
from mt in t.maintransaction
join st in t.subtransaction
on mt.MainTransactionId equals st.MainTransactionId
into sts
where mt.IsEnabled
where sts.Sum(x => x.Amount) - mt.Amount != 0
select new
{
MainTransaction = mt,
Subtransactions = sts,
};

LINQ: Build a where clause at runtime to include ORs ( || )?

I need to build a where clause at runtime but I need to do an OR with the where clause. Is this possible?
Here is my code. Basically "filter" is a enum Bitwise, son hence filter could be equal to more than 1 of the following. Hence I need to build up the where clause.
If I execute the WHEREs separately then imagine if I do the Untested first, and it returns 0 records that means I can't execute a where on the Tested because its now 0 records.
I will put some pseudo-code below:
string myWhere = "";
if ((filter & Filters.Tested) == Filters.Tested)
{
if (myWhere != "" ) myWhere =myWhere + "||";
myWhere = myWhere " Status == "Tested";
}
if ((filter & Filters.Untested) == Filters.Untested)
{
if (myWhere != "" ) myWhere =myWhere + "||";
myWhere = myWhere " Status == "Untested";
}
if ((filter & Filters.Failed) == Filters.Failed)
{
if (myWhere != "" ) myWhere =myWhere + "||";
myWhere = myWhere " Status == "Failed";
}
// dataApplications = a List of items that include Tested,Failed and Untested.
// dataApplication.Where ( myWhere) --- Confused here!
Is this possible?
I don't want to include lots of "IFs" because there are lots of combinations i.e. no filter, filter= tested Only, filter = Untested and Tested ... and lots more.
If you have this:
IEnumerable<MyType> res = from p in myquery select p;
You can define a
var conditions = new List<Func<MyType, bool>>();
conditions.Add(p => p.PropertyOne == 1);
conditions.Add(p => p.PropertyTwo == 2);
res = res.Where(p => conditions.Any(q => q(p)));
And now the trick to make Lists of Funcs of anonymous objects (and you can easily change it to "extract" the type of anonymous objects)
static List<Func<T, bool>> MakeList<T>(IEnumerable<T> elements)
{
return new List<Func<T, bool>>();
}
You call it by passing the result of a LINQ query. So
var res = from p in elements select new { Id = p.Id, Val = p.Value };
var conditions = MakeList(res);
var statusTexts = new List<string>(); // Add desired status texts
dataApplication.Where(item =>
statusTexts.Any(status => item.Status == status))
Use HashSet<> for statuses, then .Contains will be O(1) instead of usual O(n) for List<>:
var statuses = new HashSet<string>() {"a", "b", "c"};
var list = new[] {
new { Id = 1, status = "a"},
new { Id = 2, status = "b"},
new { Id = 3, status = "z"}
};
var filtered = list.Where(l => statuses.Contains(s => l.status == s));

Resources