Entity Framework - NotSupportedException - asp.net

var depts = ctx.Departments
.OrderBy(d => d.deptName)
.Select(d => d.deptNo);
foreach (int deptNumber in depts) {
var deptReports = from d in ctx.Departments
join r in matchingIncidents on d.deptNo equals r.deptNo
where r.deptNo == deptNumber
select r;
int deptReportsCount = deptReports.Count();
I am completely baffled! All the questions about this error say to use == on primitive fields (such as IDs), which I'm doing. Anything I do to this query generates the exception. The exact same code worked before and I don't know what I've done to it! Could someone please explain to me what's going on?
Also, I remember there being an EntityFramework class with methods that allowed you to convert objects within a query (e.g. dates), does anyone know what this class is?
UPDATE:
Here are the changes I made (it now works).
var deptReports = from r in matchingIncidents
join d in ctx.Departments on r.deptNo equals d.deptNo
where r.deptNo == deptNumber
select r;

matchingIncidents looks like a local collection of a complex type (because you are using r.deptNo). That's not allowed in LINQ to Entities. You could try this instead:
foreach (int deptNumber in depts) {
var deptReports = from d in ctx.Departments
join r in matchingIncidents.Select(m => m.deptNo)
on d.deptNo equals r
where r == deptNumber
select r;
int deptReportsCount = deptReports.Count();
matchingIncidents.Select(m => m.deptNo) is now a local collection of primitive types and deptReports is a sequence of int (assuming that deptNo is of type int). But for counting the resulting elements it should be still fine.
Edit
And you are probably searching for the static EntityFunctions class:
http://msdn.microsoft.com/en-us/library/system.data.objects.entityfunctions.aspx

Could the issue be:
join r in matchingIncidents on d.deptNo == r.deptNo

Related

Equivalent IN operator in entity framework

var query = from c in context.Albums
where c.AlbumID in albumIds
select c.Albumname;
Here albumIds is IENUM<> of some custom type(in my case its an Entity)
When I do the above query I get an error
Operator == cannot be applied to type int and IEnumerable
This error is acceptable but how do I overcome this
Turn it around slightly - you want to check whether your enumerable of album ids contains the id of an album you have just iterated to in your linq. So something like this ...
albumIds = {1,2,13,25,277,567};
var query = context.Albums.Where(x=> albumIds.Contains(x.ID));
(Sorry, writing example code without a tool in front of me so forgive any obvious mistakes. Hopefully you can get the idea from this though).
var albumIds= new string[] { "900", "801", "802", "803","906" };
var lstData = context.tbl.Where(
x => (x.TimeCreated >= yesterday && x.TimeCreated < today) &&
albumIds.Contains(x.TransactionSetId)
).Select(x => x.X12_Interchange).ToList();

Technique to reduce load on server,while searching & comparing for particular item into large(10,000 entries) LIST object created at server side

I want to know that,
If we have LIST object created at server side which contains large amount of data entries like employess master data(10,000), & I want to give search option to search valid employee ID or name.
So I have tried to compare that entered text with that list of large entries in loop, which is obvious degrading performance.
So is there any option to better performace?
Thanks in advance.
Try this:
public List<Employee> SearchEmployee(string search, int pageNo, int pageLength)
{
MasterDataContext db = new MasterDataContext();
var searchResult = (from e in db.Employess
where (search == null ||
e.Name.ToLower().Contains(search.ToLower()))
select e).ToList();
int pageStart = (pageNo - 1) * pageLength;
var pageResult = from c in searchResult.Skip(pageStart).Take(pageLength)
orderby c.CardNo
select c;
return pageResult;
}
I hope it helps.

Converting from Sql to Linq

I have, what I thought was a pretty straight-forward query.
In normal Sql this would read:
SELECT [column names]
FROM agentscheduledetail
WHERE (date = '2012-07-04') AND
(
exception = 'Break (No Sign Off)' OR
exception = 'Break' OR
exception = 'Break (Signed Out)'
)
This returns approx 900 records.
However, when I try to enter this into my controller, I end up with around 300,000 records - so I think my AND and ORs are not working. I've tried Linqer, but can't get it to work (I'm aware this may not be actual LINQ but the equivalent query in VS - if there is a linq version... I'd be grateful for that too if possible).
My controller query is:
var dte = DateTime.Today;
return View(db.agentscheduledetails.Where
(
d => d.date == dte && d.agentName.StartsWith("ta") &&
(
d.exception == "Break (No Sign Off)" ||
d.exception == "Break" ||
d.exception == "Break (Signed Out)"
)
).ToList()
);
Can anyone either a) let me know where I'm going wrong with my && || (and/or), or b) is there a way of stepping through the code in VS, to see what the above query translates to in normal SQL so I can try to figure out where I'm going wrong?
Thanks for any help,
Mark
The following is perhaps a simplified version of what you are trying to do, also your LINQ contains an additional statement compared to the SQL where it is comparing the agent name?
var currentDate = DateTime.Today;
var exceptionTypes = new List<string>() { "Break (No Sign Off)",
"Break", "Break (Signed Out)" };
db.agentscheduledetails.Where(d => d.date == currentDate &&
exceptionTypes.Contains(d.exception));
One thing that you could try is getting hold of a copy of LinqPad, this will let you run your LINQ statement against a database and will show you what the generated SQL statement is.
Aside from anything else,
d.agentName.StartsWith("ta")
does not appear in your original sql...?

Add Conditional Join Dynamically with Linq

I have a basic search control which lists companies from a CRM depending on predefined search/filtering criteria supplied by dropdowns. The default selection is "ALL" for each DropDown, otherwise the user chooses a specific item(s). I'd like to be able to construct a Linq query dynamically based on the selections. Out of the 5 selectors they supply values that I can match against the Company table, but two of the selectors (if either or both are chosen) would require a join or joins, else no action should be taken again the base result set. I hope this makes sense.
I'm not sure how to do this effectively. Here is my code:
private void Search()
{
EnergyPubsCRMDataContext dc = new EnergyPubsCRMDataContext();
var results = (from c in dc.Companies
select c);
//only create the join if the selected index > 0
if (ddlIndustry.SelectedIndex > 0)
{
//A company can be in 1 or more industries, thus here I want to join
//with the CompanyIndustry table and have a WHERE clause to match on the ddlIndustry.SelectedValue
}
//only create the join if the selected index > 0
if (ddlServices.SelectedIndex > 0)
{
//A company can offer 1 or more services. Here I want to join to the CompanyService table
//on the CompanyID and have a WHERE clause to match the ddlServices.SelectedValue
}
//These work OK to shape the overal query further (they don't need joins)
if (ddlCountry.SelectedIndex > 0)
results = results.Where(c => c.CountryID == Convert.ToInt32(ddlCountry.SelectedValue));
if (ddlStateRegion.SelectedIndex > 0)
results = results.Where(c => c.StateRegionID == Convert.ToInt32(ddlStateRegion.SelectedValue));
if (ddlAccountManagers.SelectedIndex > 0)
{
Guid g = new Guid(ddlAccountManagers.SelectedValue);
results = results.Where(c => c.UserId == g);
}
results = results.OrderBy(c => c.CompanyName);
//Bind to Grid....
}
if (ddlIndustry.SelectedIndex > 0)
{
//A company can be in 1 or more industries, thus here I want to join
//with the CompanyIndustry table and have a WHERE clause to match on the ddlIndustry.SelectedValue
results = results.Where(c => c.CompanyIndustry.IndustryID == ddlIndustry.SelectedValue);
}
Assuming you have correct foreign keys in your database/DBML.
This will generate the join implicitly.
I had very similar issue and no foreign keys I could leverage.
My solution would translate to something like this:
results = results
.Join(dc.CompanyIndustry, c => c.CompanyID, ci => ci.CompanyID, (c, ci) => new { c, ci.IndustryID })
.Where (a => a.IndustryID == ddlIndustry.SelectedValue)
.Select(a => a.c);
Basically:
1) first we create a join, with a projection that gives us IndustryID (join)
2) we filter based on IndustryID (where)
3) we return original anonymous type, so that we can modify original query (select)

Can't use foreach on a anonymous type

I have the code below where I am trying to go through the child questions of my qestAires anonymous type. When I get to the foreach loop i get the error:
foreach statement cannot operate on
variables of type 'Question' because
'Question' does not contain a public
definition for 'GetEnumerator'
What do I need to do differently to get around this?
var questAires = (from qs in dc.Questionnaires
from q in dc.Questions.Where(t => t.QuestionnaireId == qs.QuestionnaireID)
from r in dc.Responses.Where(qr => qr.QuestionID == q.QuestionId).DefaultIfEmpty()
where qs.QuestionnaireID == QuestionnaireId
select new
{
qs.Description,
Questions = q,
Responses = r
}).Single();
foreach(var question in questAires.Questions)
{
}
questAires.Questions will only resolve to a single question, and you will get one questAires object for each question (which will cause .Single() to throw).
I guess you want something like this:
var questAires = (from qs in dc.Questionnaires
select new {
qs.Description,
Questions = from q in dc.Questions where q.QuestionnaireId == qs.QuestionnaireID
select new {
Question = q,
Response = (from r in dc.Responses where r.QuestionID == q.QuestionId select r).DefaultIfEmpty()
}
}).Single()
q actually resolves to a single item from the enumerable dc.Questions.Where(...), so yeah, you're only going to get a single item - not an enumerable - for Questions.

Resources