LINQ One to Many, 3 levels Deep Question - c#

I am trying to figure out what I am doing wrong in the below LINQ statement. It doesn't like the third SELECT. It finds tblAddresse.tblAdminCounty in Intelisense when I am typing the query but when I type the SELECT after it it freaks.
Does it have to do with how tblAddress and tblAdminCounty are related? I would have thought that the fact it shows in Intellisense under tblAddress would make that statement self-evident but obviously not.
If I was to query just the CountyName in a seperate function it would look like this -->
var countyName = from adminCounty in context.tblAdminCounties
where adminCounty.CountyID == countyID
select adminCounty.CountyName;
And this is the larger 3-tiered approach based on this site --> HERE
var query = from tblBusinesse in context.tblBusinesses
where tblBusinesse.BusinessID == businessID
select new
{
tblBusinesse.BusinessName,
tblBusinesse.ContactName,
tblBusinesse.EmailAddress,
Address = from tblAddresse in tblBusinesse.tblAddresses
select new
{
tblAddresse.AddressLine1,
tblAddresse.AddressLine2,
tblAddresse.AddressLine3,
tblAddresse.CityName,
County = from adminCounty in tblAddresse.tblAdminCounty
select new
{
adminCounty.CountyName
}
}
};

You're trying to query it as if a single address has multiple counties. Doesn't the fact that it's called tblAdminCounty rather than tblAdminCounties suggest that it's just a single item?
Try changing this:
County = from adminCounty in tblAddresse.tblAdminCounty
select new
{
adminCounty.CountyName
}
to just:
County = tblAddresse.tblAdminCounty

Related

Grouping Table Data As Column Headers on Excel Sheet

I have a Questionnaire application that I am building for one of our teams and I have having an issue with making the report like they would like it (if at all possible). What I currently have is:
var completedSurveys = (from s in db.SurveyResponses
join d in db.Demographics on s.SurveyID equals d.SurveyID into grpjoin
from d in grpjoin.DefaultIfEmpty()
select new
{
Survey_ID = s.SurveyID,
Survey_Date = s.Survey.SurveyDate,
Question = s.Questions.QuestionTxt,
Response = s.Responses.ResponseTxt,
Zip_Code = d.ZipCode,
Department = d.CityDepartments.Department,
Ace_Score = s.Survey.AceScore,
}).ToList();
which after running through the gridview/excel code produces:
My Current Excel Sheet
and what they would like is an excel sheet that has the Questions as column headers along with SurveyID, Date, Zip, Department & Score with the responses to the questions as the row data that way everything shows only once. Something like this excel sheet I got from somewhere else:
Example
I've tried multiple different groupings for example:
var completedSurveys = (from s in db.SurveyResponses
join d in db.Demographics on s.SurveyID equals d.SurveyID into grpjoin
from d in grpjoin.DefaultIfEmpty()
group new {s.Questions.QuestionTxt, s.Survey.SurveyDate } by new { s.SurveyID, s.Responses.ResponseTxt, d.ZipCode, d.CityDepartments.Department, s.Survey.AceScore } into g
orderby g.Key.SurveyID ascending
select new
{
Survey_ID = g.Key.SurveyID,
Survey_Date = g.Select(s => s.SurveyDate),
Response = g.Key.ResponseTxt,
Zip = g.Key.ZipCode,
Department = g.Key.Department,
Ace_Score = g.Key.AceScore
}).ToList();
but I'm still not quite getting what I want. If I have to tell them that what I've already got is as good as it gets, then that's fine but I thought I would at least reach out for some advice. Any Assistance would be appreciated.
Thanks
You've got a few unknowns in your question, but if the crux of it is "how do you pivot data in c#", then I've got good news and bad news. The good news is that it's doable. The bad news is that it's not pretty.
First, you would want to group your "completedSurveys" by surveyId:
var groupedSurveys =
completedSurveys
.GroupBy(g => g.Survey_ID);
Then create a class of what you'd like your individual row to look like:
public class survey {
public int Survey_ID;
public DateTime date;
public int Zip_Code;
public int Ace_Score;
public string Q1;
public string Q2;
public string Q3;
}
Then, create a home for such objects:
var surveys = new List<survey>();
Then, loop through the groupings in the grouped surveys. We'll call this the outer loop.
In each outer loop:
create a new instance of "survey"
the common data to all items will exist in the first item, so you could just populate using it for this purpose. If nulls get in your way, then you'll have to do the appropriate logic inside the inner loop (described next)
loop through the individual items in your grouping (the inner loop)
in the inner loop, use a switch statement to map the item in the group to the appropriate field in the "survey" class using the expected question text.
when done with the inner loop, push the survey instance to the surveys home.
move on to the next outer loop iteration
Sample code for this is as follows:
foreach (var surveyItems in groupedSurveys) {
var firstItem = surveyItems.FirstOrDefault();
var survey = new survey() {
Survey_ID = firstItem.Survey_ID,
date = firstItem.Survey_Date,
Zip_Code = firstItem.Zip_Code,
Ace_Score = firstItem.Ace_Score
};
foreach(var item in surveyItems) {
switch (item.Question) {
case "Text for question 1":
survey.Q1 = item.Response;
break;
case "Text for question 2":
survey.Q2 = item.Response;
break;
case "Text for question 3":
survey.Q3 = item.Response;
break;
}
}
surveys.Add(survey);
}
You'll see that your surveys object now has pivoted data, with one "row" for each survey.
However, this is all without knowing about your excel writing code. It might be wiser to keep things "normalized" here and parse it over there. You can also consider pivoting at the server level. Pivot statements there are probably going to be a little friendlier.
Also, I don't want to cut into your logic to create "completedSurveys". But suffice it to say, if you cut it up, you might be able to make it look more elegant as a whole.

Why am I getting a compile error on the ToList() and how do you separate each sql column into a sparate list element?

String priceID = codeArray[0];
var priceResult = from PRICE_LIST in priceContext.PRICE_LIST
where PRICE_LIST.PRICE_ID == priceID
select new
{
PRICE_LIST.RETAIL,
PRICE_LIST.WHOLESALE
}.ToList();
I'm receiving a compile error that the anonymous type does not contain a definition for ToList() and I'm not sure why? I've seen many examples where queries are stored using this method. I can do priceResult.ToList(), but both the retail and wholesale price columns are in the same list element. So I can't get retail by selecting the first element ect. New to EF & LINQ and still on the learning curve.
You need to put the entire LINQ within parentheses like this:
var priceResult = (from PRICE_LIST in priceContext.PRICE_LIST
where PRICE_LIST.PRICE_ID == priceID
select new
{
PRICE_LIST.RETAIL,
PRICE_LIST.WHOLESALE
}).ToList();
otherwise it tries to make each new object to a list

LINQ Query Not Returning one value that should be there

In a block of code I have a Foreach that I use to run through and count specific pieces that may or may not exist in the database. Basically for each part on an Order, I go on to count the Product Groups those belong to and then the Division those Product Groups belong to. For that I use this LINQ query:
foreach (var OrderDtl_yRow in ( from ThisOrderDtl in Db.OrderDtl
join ThisProdGrup in Db.ProdGrup on
ThisOrderDtl.ProdCode equals ThisProdGrup.ProdCode
where
ThisOrderDtl.Company == Session.CompanyID &&
ThisOrderDtl.OrderNum == 195792
select new
{
ProdCode = ThisOrderDtl.ProdCode,
Division = ThisProdGrup.Division_c,
OrderNum = ThisOrderDtl.OrderNum,
OrderLine = ThisOrderDtl.OrderLine
}))
{ ....counting things... }
Currently I've got message boxes set up to return the values to me as the process is going. I get everything to return correctly except the Division, that always shows up as blank in the MessageBoxes (So NULL I'd assume). So my Counters for Division don't Increment.
If I take that out into LINQPad I'm unsure how to return results of a foreach, but I tried it with
if(OrderDtl_yRow.Division != null && OrderDtl_yRow.Division != "")
{i++;}
i.Dump();
and got 5 (There were 5 rows I expected so I'm at least pulling our something). Then I converted it to a simpler FirstOrDefault statement to test a single value like
var OrderDtl_yRow = ( from ThisOrderDtl in OrderDtl
join ThisProdGrup in ProdGrup on
ThisOrderDtl.ProdCode equals ThisProdGrup.ProdCode
where
ThisOrderDtl.OrderNum == 195792 &&
ThisOrderDtl.OrderLine == 1
select new
{
ProdCode = ThisOrderDtl.ProdCode,
Division = ThisProdGrup.Division_c,
OrderNum = ThisOrderDtl.OrderNum,
OrderLine = ThisOrderDtl.OrderLine
}).FirstOrDefault();
Then if I do a OrderDtl_yRow.Dump() I get my result and sure enough, Division comes through. So all signs point to it being fine, yet I can't bring over the value where I actually need it to show up. Thoughts? Thanks!
P.S. For those familiar with Epicor ERP Division is a UD field, so it technically belongs to the table ProdGrup_UD, but in Epicor it recognized that as the table ProdGrup just fine, its only SQL that makes you join _UD to the parent table. I tried joining it anyways for funsies and it didn't like it because it knew the column was there already. So that should be fine.
UPDATE: Rookie Move, didn't upload the Division data into the testing environment, so nothing was there, then checked against Live data where it existed and scratched my head as to why it didn't match. But I learned something about LinqPad and Linq so it wasn't a useless exercise.
You need to play some more in Linqpad to see what is happening, Set the language to C# program, Press F4 and add references to Server\Bin\Epicor.System.dll and Server\Assemblies\Erp.Data.910100.dll and point the app.copnfig to your Server\web.config file. In the main block create yourself a Db context with var Db = new Erp.ErpContext();
Linqpad can display complex data structures so you needn't have done FirstOrDefault in your last example. for instance:
void Main()
{
var Db = new Erp.ErpContext();
var sessionCompany = "EPIC06";
var x = (from hed in Db.OrderHed
join dtl in Db.OrderDtl
on new { hed.Company, hed.OrderNum }
equals new { dtl.Company, dtl.OrderNum }
into dtlList
where
hed.Company == sessionCompany
select new { hed, dtlList })
.Dump();
}
Also note that in SQL dbo.ProdGrup is an autogenerated view that joins the tables Erp.ProdGrup and Erp.ProdGrup_UD for you.

Create a query using LINQ to entities with 1:N relation

I know it's not something unusual to make such kind of queries but I think I get lost so I seek help. I have to tables with relation 1:N and to make it more clear I'll post a print screen from the management studio :
I am working on a asp.net mvc 3 project and I need to make a view where all Documents will be shown (and some filter and stuff, but I think that is irrelevant for this case). I need the data from the table Documents and only one specific record for each document from the DocumentFields table. This record is the record holding the name of the Document and it's uniqueness is DocumentID == Docmuents.Id, DocumentFields.RowNo == 1 and DocumentsFields.ColumnNo == 2. This is unique record for every Document and I need to get the FieldValue from this record which actually holds the Name of the Document.
I am not very sure how to build my query (maybe using JOIN) and I also would like to make my view strongly typed passing a model of type Documents but I'm not sure if it's possible, but I think depending on the way the query is build will determine the type of the model for the view.
I believe what you want is something like this:
var results =
from d in dbContext.Documents
join df in dbContext.DocumentFields
on new { d.Id, RowNo = 1, ColumnNo = 2 } equals
new { Id = df.DocumentId, df.RowNo, df.ColumnNo }
select new
{
Document = d,
DocumentName = df.FieldValue
};
Of course if you set up navigation properties, you can just do this:
var results =
from d in dbContext.Documents
let df = d.DocumentFields.First(x => x.RowNo == 1 && x.ColumnNo == 2)
select new
{
Document = d,
DocumentName = df.FieldValue
};

LINQ with ManyToMany: Filtering based on multiple selection

I am a newbe to C# and have to use it for my master thesis. At the moment, I am facing a problem that is a bit to complex for me.
I have set up a database with a many-to-many relationship like this:
Table Relay:
- id (PK)
- Name
- Input
Table ProtectionFunction:
- id (PK)
- ANSI
- IEC
- Description
Table RelayConfig (junction table)
- RelayID (PK)
- ProtFuncID (PK)
- TimeToSaturate
- Remanence
The thing is, a Relay can have multiple protection functions, and for each it has specific values for TimeToSaturate and Remanence. Now I want to realize a filter. The user can select protection function via checkboxes in a DataGridView and a ListBox should show all Relays that support ALL of these protection functions.
I have already created the LINQ-to-SQL classes for my project. But now I am stuck because I don't know how to realize the filtering. All LINQ commands I have found so far would give me all Relays for one protection function.
I really hope one of you can give me a hint.
var ids = new int[]{ ... };
// if ids is null or ids.Length == 0 please return null or an empty list,
//do not go further otherwise you'll get Relays without any function filter
var query = Relays.AsQueryable();
foreach (var id in ids)
{
var tempId = id;
query = query.Where(r=>r.RelayConfigs.Any(rc=>rc.ProtFuncID == tempId));
}
var items = query.ToList();
Update
Just saw this on PredicateBuilder page:
The temporary variable in the loop is required to avoid the outer
variable trap, where the same variable is captured for each iteration
of the foreach loop.
It's easier if you start from the RelayConfigs. Something like this should work:
var protFuncIds = new[]{1,2,3};
var query = from rc in db.RelayConfigs
where protFuncIds.Contains(rc.ProtFuncID)
select rc.Relay;
var relays = query.Distinct().ToList();
UPDATE:
based on your comment, the following should work, however do monitor the SQL generated...
IQueryable<Relay> query = db.Relays
foreach (var id in ids)
query = relays.Where(r => r.RelayConfigs.Select(x => x.ProtFuncId).Contains(id));
var relays = query.ToList();
// Build a list of protection function ids from your checkbox list
var protFuncIDs = [1,2,3,4];
using(var dc = new MyDataContext())
{
var result = dc.Relays.Where(r=>protFuncIDs.Join(r.RelayConfigs, pf=>pf, rc=>rc.ProtFuncID, (pf,rc)=>pf).Count() == protFuncIDs.Length).ToArray();
}
It's not especially efficient, but that should do the trick for you.
I have done this in Lightswitch, and here was my preprocess query:
partial void UnusedContactTypesByContact_PreprocessQuery(int? ContactID, ref IQueryable<ContactType> query)
{
query = from contactType in query
where !contactType.ContactToContactTypes.Any(c => c.Contact.Id == ContactID)
select contactType;
}
Hope that helps.

Categories