C# forming Linq Query using .FindByExp - c#

Table 1
Id Name
1 Tank 1
2 Tank 2
3 Tank 3
4 Tank 4
Table 2
Id Name
1 Tank 1
2 Tank 2
Result List it should return
Id Name
1 Tank 1
2 Tank 2
3 Tank 3
4 Tank 4
I need to form a linq query using .FindByExp only. Like below i need to use joins and arrive at above list Result.
var tankLst = context.Tank.FindByExp(a => a.Id== S.Id).ToList();

var distinctNames = context.Table1.FindAll().Select(x=>x.Name).Distinct().ToArray();
var tab1 = context.Table1.FindAll().ToList();
var tab2 = context.Table2.FindAll().Where(x=> !distinctNames.Contains(x.Name)).ToList();
var resultList = tab1.Concat(tab2).OrderBy(x=>x.Name);

Related

LINQ Query multiple orderby of joined tables

I have following LinQ query
var CGTABLE = (from cg in DbContext.CGTABLE
join tcg in DbContext.TCGTABLE on new { cg.CGroupId } equals new { tcg.CGroupId }
where tcg.TId == TId
select new {
CGroupId = cg.CGroupId,
CGroupCode = cg.CGroupCode,
Description = cg.Description,
C = cg.C,
DisplayOrder = cg.DisplayOrder
}).ToList();
CGTABLE = CGTABLE.OrderBy(g => g.DisplayOrder).ThenBy(g => g.C.OrderBy(c => c.CCode)).ToList();
which runs fine, but it is not doing second orderby using ThenBy ThenBy(g => g.C.OrderBy(c => c.CCode) What am I missing?
Sample data for better understanding.
Data in Tables
2
1
2
4
3
1
4
5
2
1
3
3
1
Should output after both outer and inner list ordered by
1
1
2
3
4
2
1
2
4
5
3
1
3
But Currently it is showing
1
4
5
2
1
2
1
2
4
3
3
3
1
You didn't want to order the main list, you are looking for a way to order inner list inside of outer one, I think.
So below code will do it for you:
var CGTABLE = (
from cg in DbContext.CGTABLE
join tcg in DbContext.TCGTABLE on new { cg.CGroupId } equals new { tcg.CGroupId }
where tcg.TId == TId
select new {
CGroupId = cg.CGroupId,
CGroupCode = cg.CGroupCode,
Description = cg.Description,
C = cg.C.OrderBy(x => x.CCode),
DisplayOrder = cg.DisplayOrder
}).ToList();
CGTABLE = CGTABLE.OrderBy(g => g.DisplayOrder).ToList();

entity linq for top 100 complicated query

i have the following structure in a table:
UniqueId Type size RelatedId
--------------------------------------------
1 honda 100 1
2 mercedes 200 3
3 mazda 300 4
4 mercedes 150 3
5 merecedes 200 3
6 ford 180 2
7 mercedes 900 5
and so on ....
I want to write a query to get :
The Top 3 items grouped by ONLY honda or mercedes ordered by their size.
Also, when considering rows for a particular Type, there could be multiple rows which may have the same RelatedId, if thats the case, i want to consider the row which is the max UniqueId for that specific RelatedId.
For eg: in the table below, i have 3 rows with RelatedId = 3 for mercedes type,
2 mercedes 200 3
4 mercedes 150 3
5 mercedes 200 3
In this case, I want to consider only the row with UniqueId = 5 as that is the latest.
In my result set for group type : "mercedes", i would expect my result set to get :
mercedes :
7 mercedes 900 5
5 merecedes 200 3
I was able to write something like this:
items.GroupBy( x => x.Type).SelectMany(g => g.OrderByDescending(r => r.size)).Take(10).
How do i group by so that ONLY honda or mercedes are considered?
Also, this includes rows which could have the same RelatedId...(i want the row which has the highest UniqueId in that case for these rows) how do i change the query to account for this?
My expected final result set:
1 honda 100 1
7 mercedes 900 5
5 merecedes 200 3
thanks everyone.
Well the first thing is add a Where to your query to get only Honda and Mercedes
items.Where(e=>e.Type=="honda" || e.Type=="mercedes")...
Now for the second part you can group by multiple columns:
items.Where(e=>e.Type=="honda" || e.Type=="mercedes")
.GroupBy( x => new{x.Type,x.RelatedId})
.Select(g => g.OrderByDescending(r => r.UniqueId).FirstOrDefault())
.OrderByDescending(r => r.size)
.Take(10);
How do i group by so that ONLY honda or mercedes are considered?
items.Where(x => x.Type == "honda" || x.Type == "mercedes").GroupBy(etc...)

Get all the values from the table that match values in an array?

I have an array like this
string[] Reportable = { "1", "3", "5" };
I have a table Products in my database with a column PartNumber and I can get all partNumbers with e.g value 2 like this.
var result = db.Products.ToList().Where(product => product.PartNumber == 2);
But I want to get all values from the database that match values in my array Reportable. Eg if my table look like this
ID Name PartNumber
1 Name1 1
2 Name2 1
3 Name3 2
4 Name4 3
5 Name5 4
6 Name3 5
The result from my query should be rows with id 1,2,4,6 because I don't have number 2 and 4 in my array.
Edit
I'am trying to get all PartNumbers that starts with a number in the Reportable array, not only exact match. If I have the number 20 in my array, valid numbers from the query would be e.g 2010, 20221, 20 and 20111 but not e.g 25111, 10333, 11000. I have found the extension method StartsWith() but I do not get it to work.
You can use Contains like this:
var result =
db.Products.Where(product =>
Reportable.Any(r => product.PartNumber.StartsWith(r))
).ToList();

Recursive get all childrens using LINQ C#

i want to show all the parentID as possible using linq for example:
if i select "submodule_idparent = 6" return all 5 and return 1. Because, to show 6 i need 5 and 1.
submodule_id, submodule_name, submodule_idparent
1 Articles null
2 Suppliers null
3 Adjustment 1
4 Presentations 1
5 Categories 1
6 Subcategories 5
7 Corridors 1
8 Cellars 1
9 Purchases 2
I'm try some like that with the next code (MySQL)
SELECT DISTINCT(submodule_id) FROM users_privileges LEFT JOIN modules_options USING(moduleoption_id) WHERE users_privileges.user_id = 1
but is not recursive.
Thanks in advance (y).
Try this:
var query=GetAll(6);
public IEnumerable<users_privileges> GetAll(int submodule_id)
{
var query = from c in db.users_privileges
where c.submodule_id == submodule_id
select c;
return query.ToList().Concat(query.ToList().SelectMany(t => GetAll(t.submodule_idparent)));
}
http://blog.csdn.net/q107770540/article/details/7708418

convert an sql query into linq to entities method based

i have got this query in sql server server that do what i want..
how can i convert it to linq
select t1.IDHardware,h.DescricaoHardware
from dbo.ProcessoHardware t1
INNER JOIN
(select t2.IDHardware, max(t2.IDProcessoHardware) as maxVisit
from dbo.ProcessoHardware t2
group by t2.IDHardware,t2.IDProcesso) v ON
v.maxVisit = t1.IDProcessoHardware JOIN dbo.Hardware h ON t1.IDHardware=h.IDHardware
where t1.Estado=1 AND IDProcesso=1
this is where i am now...but i am unable to figure it past this point..
var ProcHardware = (from procHardware in db.ProcessoHardwares
where procHardware.IDProcesso == IDProcesso
select new { procHardware.IDHardware, procHardware.IDProcessoHardware, procHardware.IDProcesso, procHardware.Estado } into x
group x by new { x.IDHardware, x.IDProcesso, x.IDProcessoHardware, x.Estado } into t
let Max = t.Max(g => g.IDProcessoHardware)
select new { IDHardware = t.Key.IDHardware, Estado = t.Key.Estado, t.Key.IDProcesso,IDProcessoHardware=t.Key.IDProcessoHardware,cMax=Max }).ToList().Where(t => t.Estado == 1 && t.IDProcesso == IDProcesso && t.IDProcessoHardware==Max).Select(c => new VMProcessoChooseHardware
{
IDHardware = c.IDHardware
});
i have got this table that relates the Table hardware with a table Process.. this table is called processHardware. this table is discribed by: IDProcessHardware IDProcess IDHardware State
the field state can have 3 states (1-Insert, 2-Remove,3-Substitute).. so i can i have this:
IDProcessHardware IDProcess IDHardware State
1 10 1 1
2 10 2 1
3 10 1 2
4 10 1 1
5 20 1 1
what i want to get is get the IDHardware that were inserted but not removed from process.
so by giving the IDProcess = 10 i want to get the hardware with the hardware ids 1 and 2..
IDProcessHardware IDProcess IDHardware State
1 10 1 1
2 10 2 1
3 10 1 2
4 20 1 1
in the table above by giving the IDProcess 10 , it should give me the Hardware ids 2.
Thanks in advance...
After a lot of trial and error, and a lot of search i found this link
http://jetmathew.wordpress.com/2014/01/21/select-latest-record-from-recordset-using-sql-and-linq/
the guy was trying something similar to what i want..
so i pick the linq query and transform it..
this is what i have now
var ProcHardware = (from a in db.ProcessoHardwares
group a by new { a.IDHardware, a.IDProcesso } into latest
join b in db.ProcessoHardwares on new { dt = latest.Max(itm => itm.IDProcessoHardware) } equals new { dt = b.IDProcessoHardware }
select new { ID = b.IDHardware, Estado=b.Estado,IDProcesso=b.IDProcesso }).ToList().Where(t => t.Estado == 1 && t.IDProcesso == IDProcesso ).Select(c => new VMProcessoChooseHardware
{
IDHardware = c.ID
});
it still need the rest the information discribing the hardware like the serial number , or the description.
i will post here the complete query..

Categories

Resources