Determine if record has children in LINQ to SQL - c#

I am having at hierarchical table with the structure
ID, Name, FK_ID, Sortkey
Fetching the data in LINQ to SQL is straight forward:
var list = from ls in db.myTable
where ls.FK_ID == levelId
orderby ls.sortkey ascending
select ls;
And I can traverse down the tree by linking to the next levelId.
But what I can't figure out, if there is a way in LINQ, to check if there is any children
I could probably build a view, that added a flag to each record, but I would rather do this in LINQ, if possible.
What would even be the best practice for adding such a flag in SQL?
My idea on checking each record, is not the most performance friendly solution.

If you have set up the foreign key correctly, should you not have the 1 to Many mapping properties?
i.e. You could write
var listWithChildren = list.Where(l => l.Children.Any());
or going the other direction
var listWithParent = list.Where(l => l.FK_ID != null);
or using the query expression instead of fluent
var listWithChildren = from item in list
where item.Children.Any()
select item;
as you asked in your comments for a boolean flag, you could do
var updatedList = from item in list
select new
{
Item = item,
HasChildren = item.Children.Any()
};

Related

Why is linq reversing order in group by

I have a linq query which seems to be reversing one column of several in some rows of an earlier query:
var dataSet = from fb in ds.Feedback_Answers
where fb.Feedback_Questions.Feedback_Questionnaires.QuestionnaireID == criteriaType
&& fb.UpdatedDate >= dateFeedbackFrom && fb.UpdatedDate <= dateFeedbackTo
select new
{
fb.Feedback_Questions.Feedback_Questionnaires.QuestionnaireID,
fb.QuestionID,
fb.Feedback_Questions.Text,
fb.Answer,
fb.UpdatedBy
};
Gets the first dataset and is confirmed working.
This is then grouped like this:
var groupedSet = from row in dataSet
group row by row.UpdatedBy
into grp
select new
{
Survey = grp.Key,
QuestionID = grp.Select(i => i.QuestionID),
Question = grp.Select(q => q.Text),
Answer = grp.Select(a => a.Answer)
};
While grouping, the resulting returnset (of type: string, list int, list string, list int) sometimes, but not always, turns the question order back to front, without inverting answer or questionID, which throws it off.
i.e. if the set is questionID 1,2,3 and question A,B,C it sometimes returns 1,2,3 and C,B,A
Can anyone advise why it may be doing this? Why only on the one column? Thanks!
edit: Got it thanks all! In case it helps anyone in future, here is the solution used:
var groupedSet = from row in dataSet
group row by row.UpdatedBy
into grp
select new
{
Survey = grp.Key,
QuestionID = grp.OrderBy(x=>x.QuestionID).Select(i => i.QuestionID),
Question = grp.OrderBy(x=>x.QuestionID).Select(q => q.Text),
Answer = grp.OrderBy(x=>x.QuestionID).Select(a => a.Answer)
};
Reversal of a grouped order is a coincidence: IQueryable<T>'s GroupBy returns groups in no particular order. Unlike in-memory GroupBy, which specifies the order of its groups, queries performed in RDBMS depend on implementation:
The query behavior that occurs as a result of executing an expression tree that represents calling GroupBy<TSource,TKey,TElement>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, Expression<Func<TSource,TElement>>) depends on the implementation of the type of the source parameter.`
If you would like to have your rows in a specific order, you need to add OrderBy to your query to force it.
How I do it and maintain the relative list order, rather than apply an order to the resulting set?
One approach is to apply grouping to your data after bringing it into memory. Apply ToList() to dataSet at the end to bring data into memory. After that, the order of subsequent GrouBy query will be consistent with dataSet. A drawback is that the grouping is no longer done in RDBMS.

Linq query to filter on most recent value / record

I have a 'complex' linq query I would like to improve and to understand.
(from x in tblOrder
orderby x.OrderNo
// where x.Filename is most recent filename for this order
group x by new { x.OrderNo, x.Color } into groupedByColorCode
select new
{
OrderNo = groupedByColorCode.Key.OrderNo,
ProductRef = groupedByColorCode.FirstOrDefault().ProductRef,
Color = groupedByColorCode.Key.Color,
Packing = groupedByColorCode.FirstOrDefault().Packing,
TotalQuantity = groupedByColorCode.Sum(bcc => bcc.OriQty).ToString()
}
x is an Order. I also would like to filter by Filename. Filename is a variable from tblOrder. Actually I would like to keep and keep only the orders from the most recent file.
What 'where' clause should I add to my linq query to be able to filter these last file name.
Thank you
First it's better to use orderby in the end of the query, because sorting will work quicker on the smaller set of data.
Second you should use where in the top of query, it will make smaller your set before grouping and sorting (set it after from line)
At last grouping creates dictionary with Key = new { x.OrderNo, x.Color } (in this keys) and Value = IEnumerable, and then groupedByColorCode becomes IEnumerabler of {Key, Value}. So it should stand in the end before orederby
there is MaxBy() or MinBy() if you need max or min by some criteria

Removing duplicate rows in database with primary key using Distinct()

I have some duplicate values in my database so I am using Linq to Entity to remove them with the code below. The problem is that there is an autonumber primary key in RosterSummaryData_Subject_Local, which invalidates the line var distinctRows = allRows.Distinct();
So, even if all the rows are the same, distinct won't work because the pk is different. Is there anyway to discredit the pk in the distinct? Or anyway to remove it from the query so it becomes a non issue. Just to note I want the query to return an IQueryable of my entity type so I can use the RemoveRange() method on the enttiy to remove the duplicates.
var allRows = (from subjLocal in customerContext.RosterSummaryData_Subject_Local
select subjLocal);
var distinctRows = allRows.Distinct();
if (allRows.Count() == distinctRows.Count())
{
return;
}
else
{
var rowsToDelete = allRows.Where(a => a != distinctRows);
customerContext.RosterSummaryData_Subject_Local.RemoveRange(rowsToDelete);
}
EDIT
I realized that to properly bring back distinct rows, all I have to do is select all the items except primary key:
var distinctRows = allRows
.Select(a => new {a.fkRosterSetID, a.fkTestInstanceID, a.fkTestTypeID,
a.fkSchoolYearID, a.fkRosterTypeID, a.fkDistrictID,
a.fkSchoolID, a.fkGradeID, a.fkDepartmentID,
a.fkCourseID, a.fkPeriodID, a.fkDemoCommonCodeID,
a.fkDemoCommonCategoryID, a.fkTest_SubjectID})
.Distinct();
The problem is that I cannot fetch the duplicate rows with the code below because the ! operator does not work with anonymous types(the variable distinctRows is an anonymous type because I didn't select all the columns):
var rowsToDelete = allRows.Where(a => a != distinctRows);
Any help?
you can try this:
var allRows = (from subjLocal in customerContext.RosterSummaryData_Subject_Local
select subjLocal).ToList();
var distinctRows = allRows.Distinct().ToList();
Since you will be dealing with list objects, then in your original else statement you can do this:
else
{
var rowsToDelete = allRows.Where(a => !distinctRows.Contains(a));
customerContext.RosterSummaryData_Subject_Local.RemoveRange(rowsToDelete);
}
To handle your issue with Distinct() and the autonumberID in the database, there are two solutions I can think of.
One is you can bring in the MoreLinq library, it's a Nuget package. then you can use the MoreLinq method DistinctBy():
allRows.DistinctBy(a => a.SomePropertyToUse);
Or the other route would be to use an IEqualityComparer with the regular .Distinct() Linq Method. You can check out this SO question for more info on using an IEqualityComparer in the .Distinct() method. using distinct with IEqualityComparer
maybe you need to check for each one of the fields in your customerContext.RosterSummaryData_Subject_Local to see which one is different

Partition data based on condition ("where") in a single query?

I have some data that I need to partition based on some condition, i.e.:
var trues = from item in items where MyCondition(item, blah) select item;
var falses = from item in items where !MyCondition(item, blah) select item;
Is there a cleaner way to do this in a single query and get both results back so that I don't have to repeat myself (and end up iterating over the data twice) like above?
If you don't want to iterate over the data twice, you will have to create a LINQ query that contains the true values and the false values with an indicator to which group they belong.
You can do this using a ToLookup:
var combined = items.ToLookup(x => MyCondition(x, blah));
var trues = combined[true];
var falses = combined[false];

LINQ to SQL Query Against Bridge Table

I'm having problems with a LINQ to SQL query in the following scenario:
I have items that have "Tags" applied via a bridge table.
I'm trying to filter a list of items to a subset that contain all of a specified set of tags and return the filtered list of items as the query result.
Tables Involved:
Item (ItemId, Name, ...other fields)
Tag (TagId, TagName)
Item_Tag(ItemId, TagId)
As an example if I had a list of Items with tags:
Item1 w/ (Tag1, Tag2)
Item2 w/ (Tag1, Tag2)
Item3 w/ (Tag1)
and I wanted to get all items where the item has both Tag1 AND Tag2 where the filter requirement is provided as an int[] of the required tagIds.
Assuming the Item and Tag Id's match the number at the end of the name. The filter for this example would be:
int[] tagFilterConditions = int[2]{1, 2};
var query = from i in itemList
//define filter here
where the result would be:
Item1,Item2 (excludes Item 3 b/c it isn't tagged with Tag1 AND Tag2)
I'm having a tough time figuring out how to combine these tables to apply that filter on the source list, I've tried using a predicate builder and various joins but just can't get the correct results.
Thanks, for any help...
// Query for all the items in the list
int[] itemIds = itemList.Select(item => item.ItemId).AsArray();
var query =
db.Item.Where(item =>
itemIds.Contains(item.ItemId));
// Apply each tag condition
foreach (int tagid in tagFilterConditions)
{
int temp = tagid;
query = query.Where(item =>
db.Item_Tag.Exists(item_tag =>
item_tag.ItemId == item.ItemId && item_tag.TagId == temp)));
}
I think the answer to your question is in .Contains(): http://blog.wekeroad.com/2008/02/27/creating-in-queries-with-linq-to-sql
Here's what I think is the relevant snippet from that site to your question:
int[] productList = new int[] { 1, 2, 3, 4 };
var myProducts = from p in db.Products
where productList.Contains(p.ProductID)
select p;
Hope this helps!
Here is some sql.
and here is the LinqToSql..
Got the following query to work using an anonymous type after defining the proper foreign key relationships the query was adapted from an answer on this question.
//the tagId's that the item in itemList must have
int[] tagFilterConditions = int[2]{1, 2};
var query =
itemList.Select( i=> new { i, itemTags= item.Item_Tags.Select(it=> it.TagId)})
.Where( x=> tagFilterConditions.All( t=> x.itemTags.Contains(t)))
.Select(x=> x.s);

Categories

Resources