vb.net Linq statement to c# - linq

I am trying to convert this statement in vb.net Linq:
Dim exchangeTradingCapacity = (
From ts In db_context.TRADE_SUMMARies _
Join el In db_context.EXECUTION_LISTs On ts.TRADE_ID Equals el.TRADE_ID _
And ts.TRADE_PART_INDEX Equals el.TRADE_PART_INDEX _
And ts.VERSION Equals el.TRADE_VERSION _
Join oe In db_context.ORDER_EXECUTIONs On oe.EXECUTION_ID Equals ts.CONTRIBUTOR_REF _
Where rootOrdersToInclude.Distinct.Contains(el.ROOT_ORDER_ID) _
And el.TRADE_VERSION = (From el1 In db_context.EXECUTION_LISTs _
Where el1.TRADE_ID = el.TRADE_ID _
And el1.TRADE_PART_INDEX = el.TRADE_PART_INDEX _
And el1.ROOT_ORDER_ID = el.ROOT_ORDER_ID _
Select el1.TRADE_VERSION).Max() _
Group oe By Key = oe.EXCHANGE_TRADING_CAPACITY Into Group _
Select Key
).ToList
My attempt to convert it to c# has thus far been unsuccessful:
var exchangeTradingCapacity =
(from ts in dbContext.TRADE_SUMMARies
join e1 in dbContext.EXECUTION_LISTs on
new
{
ts.TRADE_ID,
ts.TRADE_PART_INDEX,
ts.VERSION
}
equals
new
{
e1.TRADE_ID,
e1.TRADE_PART_INDEX,
VERSION = e1.TRADE_VERSION
}
join oe in dbContext.ORDER_EXECUTIONs on ts.CONTRIBUTOR_REF equals oe.EXECUTION_ID
where rootOrdersToInclude.Distinct().Contains(e1.ROOT_ORDER_ID) &&
e1.TRADE_VERSION == (from el1 in dbContext.EXECUTION_LISTs
where el1.TRADE_ID == el.TRADE_ID &&
el1.TRADE_PART_INDEX == el.TRADE_PART_INDEX &&
el1.ROOT_ORDER_ID == el.ROOT_ORDER_ID
select el1.TRADE_VERSION).Max()
group oe by oe.EXCHANGE_TRADING_CAPACITY into theKey
select theKey
).ToList();
It appears that for some reason, 'el' is out of scope when inside the subquery. Can someone point me to the error of my ways? It seems like this should work.

You don't have an alias el (letter L), only the alias e1 (number 1).

Related

Left outer join with multiple on conditions in LINQ

basically i want to check DestinationCustomerList with SA_CUSTOMER_ALLOC if any record found based on customerId and fieldId then return priority else priority = 1 , so i am using left outer join with entity but it's not working properly(returning only matching record and if SA_CUSTOMER_ALLOC doesn't have any record throwing "Object referenece error" how to handle this.
var Query = (from p in DestinationCustomerList
join p2 in Context.SA_CUSTOMER_ALLOC on new { f1 = p.CustomerId, f2 = p.FieldId } equals new { f1 = p2.CUSTOMER_ID, f2 = p2.FIELD_ID }
into temp
from x in temp.DefaultIfEmpty()
where x.TAG_ID == TagId && x.REGION_ID == RegionId
select new { p.CustomerId,p.FieldId, Priority = x == null ? 1 : x.PRIORITY }).ToList();
Instead of Priority = x == null ? 1 : x.PRIORITY
try Priority = p2 == null ? 1 : p2.PRIORITY
var Query = (from p in DestinationCustomerList
join p2 in Context.SA_CUSTOMER_ALLOC
on new
{ f1 = p.CustomerId, f2 = p.FieldId }
equals new
{ f1 = p2.CUSTOMER_ID, f2 = p2.FIELD_ID }
into temp
from x in temp.DefaultIfEmpty()
let priority = p2 == null ? 1 : p2.PRIORITY
where x.TAG_ID == TagId && x.REGION_ID == RegionId
select new { p.CustomerId,p.FieldId, Priority = priority }).ToList();

linq union group by undefined

I am calling the following function from a web service but can't get the object defined.
Dim x = (From a In _db.report _
Select ep = a.CurrentReport, CuCy = a.Current_Cycle) _
.Union _
(From b In _db.Counts _
Group b By b.Year, b.Report Into k() _ <--------(this is undefined)
Select ep = b.Report, CucY = b.Year).ToList().Take(10)
Is this the correct way to do a group by in a union query?
I would appreciate any help.
The Grouping syntax in VB is slightly different than C#. The Group By requires you to state Into Group rather than aliasing the new structure. See if the following works for you:
Dim x = (From a In _db.report _
Select ep = a.CurrentReport, CuCy = a.Current_Cycle) _
.Union _
(From b In _db.Counts _
Group By b.Year, b.Report Into Group _ <--------(this is undefined)
Select ep = Key.Report, Key.Year).ToList().Take(10)
Since you don't appear to be aggregating in the second query, you might be able to just do a distinct instead:
From b in _db.Counts
Select b.Report, B.Year
Distinct

Linq to sql query fails

I am having problem in executing a linq to sql query. Please have a look.
Here is my code:
SELECT c.client_name,
n.instrument_group_id,
n.trade_date,
sum(n.buy_qty) AS TotBuyQty,
sum(convert(float,n.buy_value) + convert(float,n.buy_brokerage)) AS TotBuyVal,
sum(n.sell_qty) AS TotSellQty,
sum(convert(float,n.sell_value) - convert(float,n.sell_brokerage)) AS TotSellVal,
sum(convert(float,n.sell_value) - convert(float,n.sell_brokerage))- sum(convert(float,n.buy_value) + convert(float,n.buy_brokerage)) AS ProfitLoss
FROM nse_fo_transaction AS n
LEFT JOIN client_master AS c ON n.client_id = c.client_id
WHERE n.client_id = 5
AND n.trade_date BETWEEN '09/01/2012' AND '09/19/2012'
GROUP BY c.client_name,
n.instrument_group_id,
n.trade_date
ORDER BY n.trade_date
How's this?
from d in dps_admin_user
join c in dps_admin_role on d.user_id equals c.user_id into gcs
from c2 in gcs.DefaultIfEmpty()
group d.firstname by c2.parent_id into gfns
where gfns.Any()
orderby gfns.Count()
select gfns
I just threw it together in LINQPad. Not very meaningful I must say, but it contains your required operators.
Why do you need such an example??
This is roughly the query you want. I couldn't test it, but it should be fairly close.
var query =
from n in nse_fo_transaction
where n.client_id == 5
where n.trade_date >= d0 && n.trade_date < d1
join c0 in client_master on n.client_id equals c.client_id into cs
from c in cs.DefaultIfEmpty()
group new { n, c } by new
{
c.client_name,
n.instrument_group_id,
n.trade_date,
} into gs
orderby gs.Key.trade_date
select new
{
gs.Key.client_name,
gs.Key.instrument_group_id,
gs.Key.trade_date,
TotBuyQty = gs.Sum(x =>
(float)x.n.buy_qty),
TotBuyVal = gs.Sum(x =>
(float)x.n.buy_value
+ (float)x.n.buy_brokerage),
TotSellQty = gs.Sum(x =>
(float)x.n.sell_qty),
TotSellVal = gs.Sum(x =>
(float)x.n.sell_value
- (float)x.n.sell_brokerage),
ProfitLoss = gs.Sum(x =>
(float)x.n.sell_value
- (float)x.n.sell_brokerage
- (float)x.n.buy_value
+ (float)x.n.buy_brokerage)
};
Dim query = (From n In tblNseFo.AsEnumerable _
Where n!client_id = intClientID _
Group Join c In tblClient _
On n!client_id Equals c!client_id Into cs = Group _
From c In cs.DefaultIfEmpty _
Group n, c By _
c!client_name, n!instrument_group_id, n!trade_date Into gs = Group _
Order By trade_date _
Select New With { _
.client_name = client_name, _
.instrument_group_id = instrument_group_id, _
.trade_date = trade_date, _
.TotBuyQty = gs.Sum(Function(x) x.n!buy_qty), _
.TotSellQty = gs.Sum(Function(x) x.n!sell_qty), _
.TotBuyVal = gs.Sum(Function(x) x.n!buy_value) + gs.Sum(Function(x) x.n!buy_brokerage), _
.TotSellVal = gs.Sum(Function(x) x.n!sell_value) - gs.Sum(Function(x) x.n!sell_brokerage), _
.ProfitLoss = (gs.Sum(Function(x) x.n!sell_value) - gs.Sum(Function(x) x.n!sell_brokerage)) - _
(gs.Sum(Function(x) x.n!buy_value) + gs.Sum(Function(x) x.n!buy_brokerage))})

Linq syntax join and group

Hello I need to join two tables (MainTransaction and Subtransaction), the problem here is I also want to get all the record of Maintransaction that's not in the Subtransaction, I am stuck in this part, how can I achieve this ?
protected object SelectMainTbl()
{
var mainIdAndSum = from st in t.subtransaction
group st by st.MainTransactionId into g
select new
{
Sum = (from r in g
select r.Amount).Sum(),
MainId = g.Key
};
var mainTbl = from main in t.maintransaction
join sub in mainIdAndSum on main.MainTransactionId equals sub.MainId
where main.IsEnabled == true && (sub.Sum - main.Amount != 0)
select main;
return mainTbl;
}
I think this is the query that you want:
from mt in t.maintransaction
join st in t.subtransaction
on mt.MainTransactionId equals st.MainTransactionId
into sts
where mt.IsEnabled
where sts.Sum(x => x.Amount) - mt.Amount != 0
select new
{
MainTransaction = mt,
Subtransactions = sts,
};

Entity Framework Metadata LINQ (Converting from C# to VB.NET)

I've got a query in C# that is working for me to query the metadata for the Entity Framework. I need to convert it to VB.NET, but I'm struggling to convert the AS keyword to "cast" meta to System.Data.Metadata.Edm.EntityType. I've tried TryCast, CType, Cast, etc.
Here's the query in C#:
var queryResult = from meta in oc.MetadataWorkspace.GetItems(System.Data.Metadata.Edm.DataSpace.CSpace)
.Where(m => m.BuiltInTypeKind == System.Data.Metadata.Edm.BuiltInTypeKind.EntityType)
from p in (meta as System.Data.Metadata.Edm.EntityType).Properties
.Where(p => p.DeclaringType.Name == entityClassType.Name
&& p.Name == propertyName)
select p;
This is the closest I've come to getting it to compile in VB.NET (the As keyword in underlined and says ')' expected:
Dim query2 = _
From meta In entityObjectContext.MetadataWorkspace.GetItems(System.Data.Metadata.Edm.DataSpace.CSpace) _
.Where(Function(m) m.BuiltInTypeKind = System.Data.Metadata.Edm.BuiltInTypeKind.EntityType) _
From p In (meta As System.Data.Metadata.Edm.EntityType).Properties _
.Where(Function(p) p.DeclaringType.Name = entity.GetType().Name _
And p.Name = propertyName) _
Select p
This is killing me. I'm so close...
You can use CType to type cast:
...
From p In CType(meta, System.Data.Metadata.Edm.EntityType).Properties _
...
Update: Looking again at the query, I would suggest using OfType() instead:
From meta In entityObjectContext.MetadataWorkspace.GetItems(System.Data.Metadata.Edm.DataSpace.CSpace) _
.OfType(Of System.Data.Metadata.Edm.EntityType)() _
From p In meta.Properties _
Where p.DeclaringType.Name = entity.GetType().Name _
And p.Name = propertyName _
Select p
Update 2: Also, it looks like GetItems() has a generic version that I suspect will return only items of your desired type:
From meta In entityObjectContext.MetadataWorkspace.GetItems(Of System.Data.Metadata.Edm.EntityType)(System.Data.Metadata.Edm.DataSpace.CSpace) _
From p In meta.Properties _
Where p.DeclaringType.Name = entity.GetType().Name _
And p.Name = propertyName _
Select p
If I were writing this query in VB.NET, I would do it like this without the lambdas. I think it's easier to read, but they way you're doing it would probably be easier for a C# developer to read.
Dim query2 = _
From meta In entityObjectContext.MetadataWorkspace.GetItems(System.Data.Metadata.Edm.DataSpace.CSpace) _
Where m.BuiltInTypeKind = System.Data.Metadata.Edm.BuiltInTypeKind.EntityType _
From p In CType(meta, System.Data.Metadata.Edm.EntityType).Properties _
Where p.DeclaringType.Name = entity.GetType().Name _
And p.Name = propertyName) _
Select p

Resources