My intersect in LINQ somehow dont seem to work. I have two excel sheets. I fetch it using LinQToExcel and (LinQToExcel does not support VisitSubQueryExpression i have to do some extra work).
List<BoardSheet> sourceTest = (from t in Boards[0]
where t["Board"] == boardName
select new CircuitSet
{
ID = string.Format(t["ID"]),
Data = string.Format(t["Data"]),
CtrlType = string.Format(t["CtrlType"]),
sys = string.Format(t["sys"]),
code = string.Format(t["code"])
}
).ToList<BoardSheet>();
List<BoardSheet> targetTest = (from t in Boards[0]
where t["Board"] == boardName
select new CircuitSet
{
ID = string.Format(t["ID"]),
Data = string.Format(t["Data"]),
CtrlType = string.Format(t["CtrlType"]),
sys = string.Format(t["sys"]),
code = string.Format(t["code"])
}
).ToList<BoardSheet>();
IEnumerable<BoardSheet> board = sourceTest.Intersect(targetTest);
board's count always returns 0. But when i iterate thro the field values of sourceTest and targetSet i see common field values.
These are instances of reference types. Intersect is using the DefaultComparer for reference types, which is ReferenceEquals. Since sourceTest has no common instances with targetTest, no results are found.
You could create a Comparer, or you could join like this:
List<CircuitSet> results =
(
from s in sourceTest
join t in targetTest
on s.Id equals t.Id
where s.Data == t.Data && s.ControlType == t.ControlType ...
select s
).ToList();
I think an easy way would be to implement IEqualityComparer<CircuitSet>. If CircuitSet's key is ID, then you could do it like this:
public class CircuitSetComparer : IEqualityComparer<CircuitSet>
{
#region IEqualityComparer<CircuitSet> Members
public bool Equals(CircuitSet x, CircuitSet y)
{
return x.ID == y.ID;
}
public int GetHashCode(CircuitSet obj)
{
return obj.ID;
}
#endregion
}
Then in your code:
IEnumerable<BoardSheet> board = sourceTest.Intersect(targetTest, new CircuitSetComparer());
GetHashCode method is tricky though, but it should be alright if my assumptions (ID being the key) are correct.
but i changed the query based on what david suggested
List<BoardSheet> sourceTest =(from s in (from t in Boards[0]
where t["Board"] == boardName
select new CircuitSet
{
ID = string.Format(t["ID"]),
Data = string.Format(t["Data"]),
CtrlType = string.Format(t["CtrlType"]),
sys = string.Format(t["sys"]),
code = string.Format(t["code"])
}
).ToList<BoardSheet>() jon tbl in (from t in Boards[0]
where t["Board"] == boardName
select new CircuitSet
{
ID = string.Format(t["ID"]),
Data = string.Format(t["Data"]),
CtrlType = string.Format(t["CtrlType"]),
sys = string.Format(t["sys"]),
code = string.Format(t["code"])
}
).ToList<BoardSheet>() on s.ID equals tbl.ID select s).ToList<BoardSheet>() ;
Related
I want to create a dynamic query with LINQ-to-SQL and everything works except for one thing: group by. I looked at the other questions but haven't found a reason so far why it does not work.
I use using System.Linq.Dynamic; with the following query (that works):
int numberOfRankedItems = 3;
string minMaxChoice = "max";
string orderBy = "";
if (minMaxChoice == "max")
{
orderBy = "queryGroup.Sum(row => row.POSITIONVALUE) descending";
} else if (minMaxChoice == "min")
{
orderBy = "queryGroup.Sum(row => row.POSITIONVALUE) ascending";
}
string minMaxFeatureColumnName = "BRANDNAME";
string selectMinMaxFeature = "new { minMaxFeature = queryGroup.Key." + minMaxFeatureColumnName + ", sumPosition = queryGroup.Sum(row => row.POSITIONVALUE)}";
var query = (from tableRow in Context.xxx
where /* some filters */
group tableRow by tableRow.BRANDNAME into queryGroup
orderby orderBy
select selectMinMaxFeature).Take(numberOfRankedItems).ToList();
The use of variables for orderby and select works fine, but for group by not no matter what I try to replace with a variable. The query actually works if I e.g. use a variable instead of tableRow.BRANDNAME but the query returns the wrong result. The goal is to be able to set the column for the grouping dynamically.
Ideas?
Thanks
Edit: there seem to be multiple issues also with the other variables. So I generalize the question a bit: how can I generalize the following query in terms of
The column to group by
The column to order by + ASC or DESC
In the select statement the columnname of the first statement (BRANDNAME)
Here is the query:
var query = (from tableRow in ctx.xxx
where /* filter */
group tableRow by new { tableRow.BRANDNAME } into queryGroup
orderby queryGroup.Sum(row => row.POSITIONVALUE) descending
select new { minMaxFeature = queryGroup.Key.BRANDNAME, sumPosition = queryGroup.Sum(row => row.POSITIONVALUE) }).Take(numberOfRankedItems).ToList();
Would be great without expression trees ;)
I have now elaborated a solution to the question:
The solution uses as before using System.Linq.Dynamic; allows now to set certain variables. In the end it returns a dictionary.
// set variables
int numberOfRankedItems;
if (queryData.NumberOfRankedItems != null)
{
numberOfRankedItems = (int) queryData.NumberOfRankedItems;
} else
{
numberOfRankedItems = 0;
}
string minMaxChoice = queryData.MinMaxChoice;
string minMaxFeatureColumnName = queryData.MinMaxFeatureColumnName;
string orderByPrompt = "";
if (minMaxChoice == "max")
{
orderByPrompt = "it.Sum(POSITIONVALUE) descending";
} else if (minMaxChoice == "min")
{
orderByPrompt = "it.Sum(POSITIONVALUE) ascending";
}
string groupByPrompt = queryData.MinMaxFeatureColumnName;
// execute query
IQueryable query = (from tableRow in Context.xxx
where /* some filters */
select tableRow).
GroupBy(groupByPrompt, "it").
OrderBy(orderByPrompt).
Select("new (it.Key as minMaxFeature, it.Sum(POSITIONVALUE) as sumPosition)").
Take(numberOfRankedItems);
// create response dictionary
Dictionary<int?, Dictionary<string, int?>> response = new Dictionary<int?, Dictionary<string, int?>>();
int count = 1;
foreach (dynamic item in query)
{
string minMaxFeatureReturn = item.GetType().GetProperty("minMaxFeature").GetValue(item);
int? sumPositionReturn = item.GetType().GetProperty("sumPosition").GetValue(item);
response[count] = new Dictionary<string, int?>() { { minMaxFeatureReturn, sumPositionReturn } };
count++;
}
return response;
I have been searching for the solution to this problem and this is what I have so far:
var ProductInfo = (from p in twd.Products
orderby p.PC
where p.DELMARK == active
select p).AsEnumerable();
var BuyersData =
(from x in db.MinimumProductInfo
where x != null
orderby x.ItemCode, x.Region
let pnote =
(from pn in db.ProductNotes
where pn != null
where x.MinimumProductInfoID == pn.MinimumProductInfoID
&& pn.NoteTypeFlag == "p"
orderby pn.NoteDate descending
select pn).FirstOrDefault()
let cnote =
(from c in db.ProductNotes
where c != null
where x.MinimumProductInfoID == c.MinimumProductInfoID
&& c.NoteTypeFlag == "c"
orderby c.NoteDate descending
select c).FirstOrDefault()
let product =
(from p in ProductInfo
where x.ItemCode == p.PC
select p).FirstOrDefault()
select new ProductInfoWithNoteList
{
MinimumProductInfoID = x.MinimumProductInfoID,
ItemCode = x.ItemCode,
EquivCode = x.EquivCode,
Description = product.PDESC,
MinimumOnHandQuantity = x.MinimumOnHandQuantity,
MaximumOHandQuantity = x.MaximumOHandQuantity,
MinimumOrderQuantity = x.MinimumOrderQuantity,
LeadTimeInWeeks = x.LeadTimeInWeeks,
Region = x.Region,
Comment = cnote.ItemNote,
PermanentNote = pnote.ItemNote
}).ToArray();
It looks correct but I am getting an error,
'The specified LINQ expression contains references to queries that are
associated with different contexts.'
What this code is supposed to do is pull out all the active product codes from the first table using the twd datacontext then use data from that database in the db.MinimumProductInfo table. The reason they have 2 separate data contexts are they are completely different databases, the first is our ERP and the second is one that we are building in house.
What am I missing? I know it is possible to do this by separating the two datacontexts then adding them together because I have seen it done with single instances but I cannot find how to do it with list data.
Instead of this:
let product =
(from p in ProductInfo
where x.ItemCode == p.PC
select p).FirstOrDefault()
select new ProductInfoWithNoteList
{
MinimumProductInfoID = x.MinimumProductInfoID,
ItemCode = x.ItemCode,
EquivCode = x.EquivCode,
Description = product.PDESC,
MinimumOnHandQuantity = x.MinimumOnHandQuantity,
MaximumOHandQuantity = x.MaximumOHandQuantity,
MinimumOrderQuantity = x.MinimumOrderQuantity,
LeadTimeInWeeks = x.LeadTimeInWeeks,
Region = x.Region,
Comment = cnote.ItemNote,
PermanentNote = pnote.ItemNote
}).ToArray();
Try this by removing the let product clause and not filling the properties associated with ProductInfo because we will do that afterwards (See I have commented out the Description property):
select new ProductInfoWithNoteList
{
MinimumProductInfoID = x.MinimumProductInfoID,
ItemCode = x.ItemCode,
EquivCode = x.EquivCode,
//Description = product.PDESC,
MinimumOnHandQuantity = x.MinimumOnHandQuantity,
MaximumOHandQuantity = x.MaximumOHandQuantity,
MinimumOrderQuantity = x.MinimumOrderQuantity,
LeadTimeInWeeks = x.LeadTimeInWeeks,
Region = x.Region,
Comment = cnote.ItemNote,
PermanentNote = pnote.ItemNote
}).ToArray();
Now that you have your BuyersData and ProductInfo in memory, set the Description property or all the items in BuyersData:
foreach(var thisBuyerData in BuyersData)
{
var thisPi = ProductInfo.SingleOrDefault(x => x.PC == thisBuyerData.ItemCode);
thisBuyerData.Description = thisPi?.PDESC;
}
I have the following View defined in my Context class, Entity Framework: I have added .Include() here, with the hopes that this will eliminate calls to database later.
public IQueryable<GeneralReportModel> vwGeneralReportItems
{
get
{
return from p in this.Patients.AsNoTracking().Include(p => p.Age) //Multiple includes added to include all properties on model
join fac in this.Facilities.AsNoTracking() on p.FacilityID equals fac.ID into fJoin
from f in fJoin.DefaultIfEmpty()
join userFac in this.UserFacilities.AsNoTracking() on p.FacilityID equals userFac.FacilityID into ufJoin
from uf in ufJoin.DefaultIfEmpty()
select new GeneralReportModel()
{
Patient = p,
//ReportIndex = ri,
Facility = f,
UserFacility = uf
};
}
}
I also have this function, which does some filtering on my select from the View:
private IQueryable<GeneralReportModel> generalReportQuery(ApplicationTypes.ReportParameterObject repParam)
{
var reportQuery = ReportHelper.GeneralReport_Source(this.DbContext, repParam, false);
reportQuery = ReportHelper.GeneralReport_FilterCriteria(this.DbContext, reportQuery, repParam);
// Lab confirmed - Extra Pulmonary
reportQuery = reportQuery.Where(rq =>
rq.Patient.TypeOfResistantTBConfirmation.Value == ApplicationTypes.ResistantTBConfirmationTypes.LABCONFIRMED.ToString() &&
rq.Patient.SiteOfDiseaseID != repParam.DatabaseSettingObject.SiteOfDiseaseID_ExtraPulmonary &&
rq.Patient.PatientCategoryID != ApplicationGlobal.GLBDATABASESETTINGOBJECT.PatientCategoryID_TransferIn);
return reportQuery;
}
The code below
var reportQueryMdr = reportQuery.Where(rq =>
rq.Patient.TypeOfResistantTB.Value == ApplicationTypes.ResistantTBTypes.MDR.ToString());
var list = reportQueryMdr.ToList();
foreach (var obj in list)
{
obj.ReportIndex = obj.Patient.getRecalculatedReportIndexFields(ApplicationGlobal.GLBDATABASESETTINGOBJECT);
}
reportQueryMdr = list.AsQueryable();
Still calls the Database for each iteration made over the list. How can I make this list entirely reside in memory, with all the Patient properties.
I want to run the following LINQ query twice but with an addition to the Where clause:
var TickList =
(from comp in Companies
join eqRes in Equity_issues on comp.Ticker equals eqRes.Ticker
where !comp.Coverage_status.Contains("dropp")
&& !comp.Coverage_status.Contains("Repla") && eqRes.Primary_equity.Equals('N')
select new
{
LocalTick = eqRes.Local_ticker.Trim(),
Exchange = eqRes.Exchange_code.Contains("HKSE") ? "HK" : (eqRes.Exchange_code.Contains("NSDQ") ? "NASDQ" : eqRes.Exchange_code),
Ticker = comp.Ticker.Trim()
}).ToList();
This query works fine, but I need to pass an additional parameter to the Where clause:
where !comp.Coverage_status.Contains("dropp")
&& !comp.Coverage_status.Contains("Repla") && eqRes.Primary_equity.Equals('N')
&& !comp.Coverage_status.Contains("Intl") <--- new addition
Is there a way to do this without being DRY? Isn't there an efficient way of doing this without repeating the query with the new addition?
// select additional Intl field (similar to Exchange)
var TickList =
(from comp in Companies
join eqRes in Equity_issues on comp.Ticker equals eqRes.Ticker
where !comp.Coverage_status.Contains("dropp")
&& !comp.Coverage_status.Contains("Repla") && eqRes.Primary_equity.Equals('N')
select new
{
LocalTick = eqRes.Local_ticker.Trim(),
Exchange = eqRes.Exchange_code.Contains("HKSE") ? "HK" : (eqRes.Exchange_code.Contains("NSDQ") ? "NASDQ" : eqRes.Exchange_code),
Intl = comp.Coverage_status.Contains("Intl") ? 1 : 0,
Ticker = comp.Ticker.Trim()
}).ToList();
// use LINQ to objects to filter results of the 1st query
var intl = TickList.Where(x => x.Intl = 0).ToList();
See the code below if you want to be DRY:
var keywords = new string[] { "dropp", "Repla", "Intl" };
var TickList = Companies
.Join(Equity_issues, c => c.Ticker, e => e.Ticker, (c, e) => new { c, e })
.Where(ce => ce.e.Primary_equity.Equals('N')
&& keywords.All(v => !ce.c.Coverage_status.Contains(v)))
.Select(ce => new
{
LocalTick = ce.e.Local_ticker.Trim(),
Exchange = ce.e.Exchange_code.Contains("HKSE")
? "HK"
: (ce.e.Exchange_code.Contains("NSDQ")
? "NASDQ"
: ce.e.Exchange_code),
Ticker = ce.c.Ticker.Trim()
})
.ToList();
Now you can run this query with any combination of keywords.
Probably overkill here but there have been situations where I've created a full blown query object that internally held an IQueryable and used methods on the object to add the where clause (mostly for letting users filter and sort their results)
public class TickList{
IQueryable<Foo> _query;
public TickList(){
_query = from comp in Companies
join eqRes in Equity_issues on comp.Ticker equals eqRes.Ticker
select new Foo {
LocalTick = eqRes.Local_ticker.Trim(),
Exchange = eqRes.Exchange_code.Contains("HKSE") ? "HK" :(eqRes.Exchange_code.Contains("NSDQ") ? "NASDQ" : eqRes.Exchange_code),
Ticker = comp.Ticker.Trim()
};
}
public void WhereCoverageContains(string text){
_query = _query.Where(x => x.Coverage_Status.Contains(text));
}
public void WherePrimaryEquityIs(string text){
_query = _query.Where(x => x.PrimaryEquity.Equals(text));
}
public List<Foo> ToList(){
return _query.ToList();
}
}
It's super verbose so use with caution. Sometimes it's possible to be too dry.
Please help. I am new in ASP.NET MVC and I am trying to send a query to a repository but it gives me an error:
Errr 3 Cannot implicitly convert type
'System.Collections.Generic.List' to
'System.Collections.Generic.IList'. An
explicit conversion exists (are you missing a cast?)
I am using a a Schema Class with only the columns that I need. This is the code that I am using for the repository.
public class SSGridRepository : SSGridIRepository
{
private DataClassesSSDataContext db;
public SSGridRepository()
{
db = new DataClassesSSDataContext();
}
public IList<SSQuerySchema> ListAll()
{
var SSQuery = (from HISTORies in db.HISTORies
join SSes in db.SSes on HISTORies.WO equals SSes.WO
join SSCUSTOMs in db.SSCUSTOMs on SSes.WO equals SSCUSTOMs.WO
join StatusTables in db.StatusTables on new { STATUS = SSes.STATUS } equals new { STATUS = StatusTables.Status }
join StatusTable_1 in db.StatusTables on new { OLDSTATUS = HISTORies.OLDSTATUS } equals new { OLDSTATUS = StatusTable_1.Status }
join StatusTable_2 in db.StatusTables on new { NEWSTATUS = HISTORies.NEWSTATUS } equals new { NEWSTATUS = StatusTable_2.Status }
where
HISTORies.OLDSTATUS == "m" &&
HISTORies.NEWSTATUS == "n" &&
HISTORies.ACTION == "Change Job Status" &&
HISTORies.OLDSTATUS != HISTORies.NEWSTATUS &&
HISTORies.DATE.Value.Year == Convert.ToDateTime(DateTime.Now).Year ||
HISTORies.OLDSTATUS != HISTORies.NEWSTATUS &&
HISTORies.NEWSTATUS == "m" &&
HISTORies.ACTION == "Checked In Work Order" &&
HISTORies.DATE.Value.Year == Convert.ToDateTime(DateTime.Now).Year
orderby
HISTORies.DATE
select new
{
HISTORies.WO,
SSes.TITLE,
SSes.DESCRIPT,
SSCUSTOMs.CUSTNAME,
SSes.STAKER,
HISTORies.USER,
SSes.STATUS,
HISTORies.OLDSTATUS,
HISTORies.NEWSTATUS,
CURRENT_STATUS = StatusTables.Description,
OLD_STATUS = StatusTable_1.Description,
NEW_STATUS = StatusTable_2.Description,
HISTORies.DATE.Value.Month,
HISTORies.DATE
}).Distinct();
return SSQuery.ToList();
}
}
In you Linq you do this:
select new {
HISTORies.WO,
SSes.TITLE,
SSes.DESCRIPT,
SSCUSTOMs.CUSTNAME,
SSes.STAKER,
HISTORies.USER,
SSes.STATUS,
HISTORies.OLDSTATUS,
HISTORies.NEWSTATUS,
CURRENT_STATUS = StatusTables.Description,
OLD_STATUS = StatusTable_1.Description,
NEW_STATUS = StatusTable_2.Description,
HISTORies.DATE.Value.Month,
HISTORies.DATE
}
Which is a dynamic type so it wont match you return type of List<SSQuerySchema>
Try initializing the type you have specified in your Linq and set the properties of course.
select new SSQuerySchema {
// initialize all properties here
}
You're trying to return an IList<SSQuerySchema> but your actual return type is IList<dynamic> (Your not selecting SSQuerySchema's but an anonymous type.
You should either make the return type of the function IList<dynamic> or modify your select to create new instances of SSQuerySchema