Linq To Nhibernate - Separated queries - linq

I've got the following situation:
var totalRecords = (from entry in invalidAccEntryRepository
select entry).Where(entry =>
entry.CustomerAccountInfo.CustomerAccountName == CustomerAccountName &&
entry.CustomerAccountInfo.CustomerAccountId == CustomerAccountId).Count();
vs.
var totalRecords = (from entry in invalidAccEntryRepository
select entry);
if (CustomerAccountInfoFilterActive)
{
totalRecords.Where(entry =>
entry.CustomerAccountInfo.CustomerAccountName == CustomerAccountName &&
entry.CustomerAccountInfo.CustomerAccountId == CustomerAccountId);
}
totalRecordsCount = totalRecords.Count();
The first query works completely but the second query only executes the only
var totalRecords = (from entry in invalidAccEntryRepository
select entry);
and ignores the conditionally added where expression.
Anyone know what the problem could be? Do you need more info?
Thx in advance!

You aren't actually applying the where. In your if, use this instead:
totalRecords = totalRecords.Where(entry =>
entry.CustomerAccountInfo.CustomerAccountName == CustomerAccountName &&
entry.CustomerAccountInfo.CustomerAccountId == CustomerAccountId);

Related

How to hide a row using LINQ C#

I have a var variable where it has set of objects. I want to query a particular object and hide/delete 2 rows based on ID.
I am trying to select the value as below but still it is not working. Any help is greatly appreciated.
var getTransformDetails = contentView.Package; if(totalCost > 100000) { getTransformDetails.AnnualPaymentGrid.Where(x => x.Id != XXXXX1 || x.Id != XXXXX2); }

LINQ EF AND VS2017

I wrote a query and worked on LINQPAD
from x in FacilityData
from y in FavInformation
where y.UserID == 1 && x.ID == y.FacilityID
select new
{
xID = x.ID,
xDistrictName = (from y in _Ilcelers
where y.ID == x.DistrictID
select y.IlceAd).FirstOrDefault(),
xName = x.Name,
Value = (from o in Tags
from p in Table_tags
where o.Prefix != null && o.Prefix == p._NAME && o.Facility == y.FacilityID
orderby p.İd descending
select new
{
FType = o.TagType,
Name = o.TagsName,
Value = p._VALUE,
Time = p._TIMESTAMP
}).Take(Tags.Count(h => h.Facility == y.FacilityID))
}
result
the result is perfect
but does not work in visual studio,
Value = (from o in DB.Tags
from p in DB.table_tags
where o.Prefix != null && o.Prefix == p.C_NAME && o.Facility == 11
orderby p.id descending
select new
{
FType=o.TagType,
Name = o.TagsName,
Value = p.C_VALUE,
Time = p.C_TIMESTAMP
}).Take(Tags.Count(h => h.Facility == y.FacilityID))
and it gives an error.
I guess the part with .Take() doesn't work because it's linq to EF.
error:
Limit must be a DbConstantExpression or a Db Parameter Reference Expression. Parametre name: count]
error image
thank you have a good day
Not sure but I will just throw it in. If you are talking about linq to ef/sql, it is possible they dont know a thing about C#. If take() would be the problem try to get the select result local first by doing .tolist(). Afterwards use your take funtion.
.ToList().Take(Tags.Count(h => h.Facility == y.FacilityID))

Dynamic where condition LINQ

I have a problem with my code. I don't know how I can insert in my selection all equals conditions of:
codicielementipartizione.sezione == el[i].ToString()
dynamically from
codicielementipartizione.sezione == el[1].ToString()
to
codicielementipartizione.sezione == el[el.count - 1].ToString()
Tn this code:
var selection = (from codicielementipartizione inlistacodici.cep
where codicielementipartizione.uno == 1 &&
codicielementipartizione.sezione == el[i].ToString()
select codicielementipartizione).ToList();
You can make the fixed part of the query which will be IQueryable. After that you can add your conditions as such.
Fixed part:
var query = from codicielementipartizione in listacodici.cep
where codicielementipartizione.uno == 1;
Dynamic part:
foreach(var condition in el)
query = query.Where(codicielementipartizione.sezione == el.ToString());
Query execution:
var result = query.Select().ToList();
Maybe you mean this:
var selection = (from codicielementipartizione in listacodici.cep
where codicielementipartizione.uno == 1
&& el.Select(i => i.ToString()).Contains(codicielementipartizione.sezione)
select codicielementipartizione).ToList();

Dynamic linq query with dynamic where conditions and from conditions

public GetApplicants(string Office,
int Id,
List<int> cfrparts,
List<int> expertiseAreaIds,
List<int> authIds,
List<int> specIds)
{
bool isAuthIdsNull = authIds == null;
authIds = authIds ?? new List<int>();
bool isSpecIdNull = specIds == null;
enter code here
var query =
from application in Apps
from cfr in application.cfr
from exp in cfr.Aoe
from auth in exp.Auth
from spec in exp.Special
where application.Design.Id == 14
where (iscfrpart || cfrPartIds.Contains(cfr.CfrP.Id))
where (isexp || expertiseAreaIds.Contains(exp.Aoe.Id))
where (isAuthIdsNull || authIds.Contains(auth.Auth.Id))
where (isSpecIdNull || specIds.Contains(spec.Special.Id))
where application.Office.Text.Contains(Office)
where application.D.Id == Id
select application.Id;
How can i make this query dynamic. If I have only Id and Office values It should still give me the resultset based on the avaliable values. Cureently its not giving me the result.
Instead of doing multiple calls to where, use &&
var query =
from Apps
where (iscfrpart || cfrPartIds.Contains(Apps.cfr.CfrP.Id))
&& (isexp || expertiseAreaIds.Contains(Apps.cfr.Aoe.Id))
&& (isAuthIdsNull || authIds.Contains(Apps.cfr.Aoe.Auth.Id))
&& (isSpecIdNull || specIds.Contains(Apps.cfr.Aoe.Special.Id))
&& Apps.Office.Text.Contains(Office)
&& Apps.D.Id == Id
select application.Id;
Additionally, this clause application.D.Id == 14 will cause 0 results when combined with this one: application.D.Id == Id if the passed in Id does not equal 14. You may want to delete that first clause.
Edit: updated your from clause, but I still don't think this will work because your table structure seems off.
Dynamic querying isn't required to solve this problem. You can write code that constructs the query.
Make a method that constructs your filter based on the information you have.
public Expression<Func<Application, bool>> GetFilterForCFR(List<int> cfrParts)
{
if (cfrParts == null || !cfrParts.Any())
{
return app => true;
}
else
{
return app => app.cfr.Any(cfr => cfrParts.Contains(cfr.cfrPId));
}
}
Then, you can construct the query using the expression.
var query = Apps;
var cfrFilter = GetFilterForCFR(cfrParts);
query = query.Where(cfrFilter);
//TODO apply the other filters
var finalquery = query.Select(app => app.Id);

Restrict records returned from nested LINQ statement

how would I restrict the "Charges" returned in this linq query, to a specified date range:
var dte = DateTime.Parse("2012-01-01");
var dte2 = DateTime.Parse("2012-02-01");
var meetingrooms = tblMeetingRoom
.Where(r => r.building_id==1)
.GroupBy(p => p.tblType)
.Select(g => new
{
TypeName = g.Key.room_type,
TypeID = g.Key.type_id,
TypeCount = g.Count(),
charges =
from rt in charges
where (rt.type_id == g.Key.type_id)
select new {
rt.chargedate,
rt.people,
rt.charge
}
});
meetingrooms.Dump();
I think it needs to go inbetween here somehwhere:
from rt in charges
where (rt.type_id == g.Key.type_id) (EG) && rt.chargedate>=dte and rt.chargedate <dte2
select new {
Thanks for any help,
Mark
from rt in charges
where rt.type_id == g.Key.type_id && (rt.chargedate >= dte && rt.chargedate <= dte2)
select new {
.....
#Maarten - I've been trying this for ages - LinqPad kept giving errors, so I was trying all different ways I could think of - for whatever reason, I must have made some typos, that I didn't make above!
Sorry for wasting everyone's time - the above works "as-is"!
Mark

Resources