Solve multi properties joining - c#

Let's assume that I'm having two tables: PropertyTypes and Properties
PropertyTypes
ID | Name
1 | Color
2 | Country
3 | Size
Properties
ID | ID PropertyTypes | Name
1 | 1 | Red
2 | 1 | Blue
3 | 1 | Green
4 | 2 | China
5 | 2 | Macau
6 | 3 | S
7 | 3 | L
For example, if I choose properties 1,2,4 5 above, if there are 2 distinct PropertyTypes, the list I want looks like (2x2=4):
Red-China
Red-Macau
Green-China
Green-Macau
If I choose all properties, here are 3 distinct PropertyTypes, the list I want which looks like(3x2x2=12):
Red-China-S
Red-Macau-S
Blue-China-S
Blue-Macau-S
Green-China-S
Green-Macau-S
Red-China-L
Red-Macau-L
Blue-China-L
Blue-Macau-L
Green-China-L
Green-Macau-L
And so on, if we add more PropertyTypes and Properties in the future
I can't figure out how to do the loop to get those result for future extending without changing the code and avoid duplicating.
Please help me if you figured out something.

Related

UnionBy() EF Core

I have this ef query that give me the following result
IQueryable<A>
| Id | count |
| 1 | 5 |
| 2 | 6 |
IQueryable<B>
| Id | count |
| 1 | 1 |
| 2 | 2 |
| 3 | 9 |
When I do
IQueryable<Something> C = A.union(B)
Result that I got is this
| Id | count |
| 1 | 5 |
| 2 | 6 |
| 1 | 1 |
| 2 | 2 |
| 3 | 9 |
Whish is logical.
What I want is a UnionBy(Id)
IQueryable<Something> C = A.unionBy(B,c=>c.Id)
and this work perfectly in my case
| Id | count |
| 1 | 5 | -- FROM A
| 2 | 6 | -- FROM A
| 3 | 9 | -- FROM B
If the Query A or B are already executed by that I mean a ToList() was made it work perfectly and I have no problem in anyway.
But in my case, both queries are not executed and thus using this function result in.
System.InvalidOperationException query could not be translated.
the alternative is to use a GroupBy however I have no idea how to replacte UnionBy behavior with the GroupBy
FYI: the query works perfectly using the IQueryable.Union
and it's mandatory in my case that the request stay in IQueryable and not executed until later
UPDATE
⚠️ The solution that I'm looking for must stay in IQueryable without a toList() execution
"query could not be translated" usually means that EF doesn't support a certain LINQ or language construct as it can't translate it into SQL. One way to make this work is to force the evaluation of the expression client-side by adding e.g. ToList() or likes on the query parts before executing the UnionBy:
IQueryable<Something> C = A.ToList().UnionBy(B.ToList(),c=>c.Id);
The solution is simple you filtre A From B using the following
IQueryable<Something> C = A.Union(B.where(b=> A.All(a=>a.Id != b.Id))

Linq : filter duplicated line without taking account a column [duplicate]

This question already has answers here:
LINQ's Distinct() on a particular property
(23 answers)
Closed 6 years ago.
I'm trying to remove from a datatable the duplicated lines, but without taking into account a column into the duplication filter.
Example :
| Name | Region |
| Toto | 5 |
| Toto | 2 |
| Toto | 1 |
| Gege | 2 |
What I'm searching for if to filter it as the following
| Name | Region |
| Toto | 5 |
| Gege | 2 |
Thank for your help.
Try this
var filteredData = data.GroupBy( d=> d.Name).SelectMany(grouping => grouping.First());
Depending upon LinqProvider for the database (IQueryable implementation), The query may run on database, or on client side( if so, there will be memory/network bandwidth issues).
In some case, not all Linq constructs are supported.
Try:
datatable.GroupBy(x => x.Name);
you need to add a using for the System.Linq namespace.

List of object into a data structure or datatable with collation

I have got a List with the following object
// Properties
long InstID
long SheetID
String[] Names
Double[] Values
The populated list contains the following items
InstID | SheetID | Names | Values |
-----------------------------------------------------------------
8 | 100 | 2,3,4,5 | 200, 300, 400, 500 |
9 | 100 | 2,3,4,5 | 2000,3000,4000,5000 |
8 | 101 | 2,3,4,5 | 201, 301, 401, 501 |
9 | 101 | 2,3,4,500 | 2001, 3001, 4001, 5001 |
I want to make use of this list and create a data structure with the following Columns and Rows
The columns Names
-----------------
SheetID
InstID_Name
Rows
-----
Instrument Values
Using the list above the output should look as below
SheetID |8_2 |8_3 |8_4 |8_5 |9_2 |9_3 |9_4 | 9_5 |
-------------------------------------------------------------------------
100 |200 |300 |400 |500 |2000 |3000 |4000 |5000 |
101 |201 |301 |401 |501 |2001 |3001 |4001 |5001 |
What I have tried
I have tried to create a datatable and try to loop through the list to create the columns and populate the rows. This seems very convoluted, I am hoping to get some ideas on how to implement this. If there is anything in .Net framework which will make this task easier.
Thanks in advance.

Possible to add two columns together and group by that column using linq on IQueryable?

My data comes from IQueryable that looks like the following table when I return all of the DailyCollectionActivities.
CatType | CdDescr | CollTypeDescr | RollCasteDescr | TIFDescr | TADescr | Amount
--------------------------------------------------------------------------------
Cat 1 | Cd 1 | CollType 233 | Roll Caste 234 | TIF 2344 | TA 2343 | 344.35
Cat 1 | Cd 1 | CollType 222 | Roll Caste 235 | TIF 2345 | TA 2344 | 355.35
Cat 2 | Cd 2 | CollType 223 | Roll Caste 236 | TIF 2346 | TA 2345 | 664.44
Cat 3 | Cd 3 | CollType 255 | Roll Caste 236 | TIF 2347 | TA 2346 | 455.34
Cat 4 | Cd 4 | CollType 266 | Roll Caste 236 | TIF 2348 | TA 2347 | 455.44
I'm trying to find out if it's possible, using linq on the IQueryable<DailyCollectionActivity> data, to add two columns together and then group on that newly created column? For example I need to figure out how to add CatType to CdDescr (CatType + '-' + CdDescr) and then group by that newly created column and sum the Amounts. Finally, I then need to take the results of that query and bind it to a RadGrid.
To make things more interesting, the user is allowed to choose which columns get added together. I could wind up with a group by clause like (CatType + '-' CdDescr), (TIFDescr + '-' + TADescr).
Is this something that I can reasonably accomplish using Linq?
say its from categories
var result = from s in (
from p in categories
select new {
TypeDesr = p.CatType+"-"+ p.CdDesr,
CollTypeDescr = p.CollTypeDescr ,
RollCasteDescr = p.RollCasteDescr,
TIFDescr = p.TIFDescr,
TADescr = p.TADescr,
Amount = p.Amount
})
group s by s.TypeDesr into r
select r;

How to use C# to parse a glossary into database?

This should be a simple one, but I'm a beginner with C#.
Given a glossary list in the following format:
aptitude
ability, skill, gift, talent
aqueous
watery
arguably
maybe, perhaps, possibly, could be
How can I parse this, and insert into a database table in the format:
TABLE: Term_Glossary
================================================
Term_Name | Term_Definition |
================================================
aptitude | ability, skill, gift, talent |
------------------------------------------------
aqueous | watery |
------------------------------------------------
arguably | maybe, perhaps, possibly, could be|
================================================
Any help would be appreciated - thanks.
Update
I realize the database structure is simple/inefficient - but really, the point of my question is the code to parse the kind of text found in the first example, using C#. Thanks.
It may seem more complex at first, but you'll find it a lot easier in the long-term to think in terms of two tables:
===========================================
Term_ID | Term_Name |
===========================================
1 | aptitude |
2 | aqueous |
3 | arguably |
===========================================
===============================================
Definition_ID | Term_ID | Definition_Name |
===============================================
1 | 1 | ability |
2 | 1 | skill |
3 | 1 | gift |
4 | 1 | talent |
5 | 2 | watery |
6 | 3 | maybe |
7 | etc.etc.etc
Perhaps even think if you can normalise this further by having one table of words with IDs and a table of associations.
It looks to me like you would read the first line, save it to a variable, read the second line, save it to a second variable, then insert into the table where Term_Name = first variable, and Term_Definition = second variable.
So your logic would be like:
StreamReader SR;
string Term_Name;
string Term_Definition
SR = File.OpenText(filename);
Term_Name = SR.ReadLine();
while(Term_Name != null)
{
Term_Definition = SR.ReadLine();
// make your database call here to insert with these two variables. I don't know what DB you are using.
Term_Name = SR.ReadLine();
}
SR.Close();

Categories

Resources