Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I am trying in this linq query which is in VB.Net.

I want to use it in c#

Can you Help Me

VB
From e In db.Stat_Elements _
                      Join a In db.tActors _
                    On a.guId Equals e.ActorGuid _
                 Where e.Value > 0 And e.Segment >= segmentFrom And e.Segment <= segmentTo And e.PeriodId >= fromPeriod And
           e.PeriodId <= toPeriod And ((a.lEANSuppLoc And accessBitmask).Equals(accessBitmask)) _
                    Group By e.DefinitionId _
                   Into Antall = Count(), Verdi = Sum(e.Value * scaleFactor), AntallSolgt = Sum(e.UnitsSold) _
                      Where Antall >= 1 And DefinitionId = definitionIdParam _
                  Select Antall, Verdi, AntallSolgt;
Posted

This Code Help Me.

C#
using System;  
using System.Collections.Generic;  
using System.Linq;  
 
namespace Aggregate  
{  
    class MyData  
    {  
        public DateTime StartDate;  
        public DateTime EndDate;  
        public string type;  
    }  
 
    class Program  
    {  
        static void Main(string[] args)  
        {  
            List<mydata> myDataList = new List<mydata>();  
 
            myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 19), EndDate = new DateTime(2008, 06, 25), type = "1" });  
            myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 20), EndDate = new DateTime(2008, 06, 25), type = "1" });  
            myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 20), EndDate = new DateTime(2008, 06, 28), type = "1" });  
            myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 22), EndDate = new DateTime(2008, 06, 25), type = "2" });  
            myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 22), EndDate = new DateTime(2008, 06, 26), type = "2" });  
            myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 23), EndDate = new DateTime(2008, 06, 24), type = "3" });  
            myDataList.Add(new MyData() { StartDate = new DateTime(2008, 06, 19), EndDate = new DateTime(2008, 06, 27), type = "4" });  
 
            var result =  
                from recod in myDataList  
                group recod by recod.type into g  
                select new {type = g.Key, StartDate = g.Min(record => record.StartDate), EndDate = g.Max(record => record.EndDate) };  
 
            foreach (var row in result)  
            {  
                Console.WriteLine("{0}\t{1}\t{2}",  
                   row.type,  
                   row.StartDate.ToString(),  
                   row.EndDate.ToString());  
            }  
 
            Console.ReadKey();  
        }  
    }  
}
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 1-Jun-12 5:48am    
Good work, +5! for sharing code..
sunandandutt 1-Jun-12 5:50am    
Thanks Prasad_Kulkarni!
Did you try at all?

Have a look at these tools:
Code Translation for .NET (C#<->VB.NET)[^]
Developer Fusion Convert C# to VB.NET [^]

There are more on the web. But they would not convert 100% successfully. These would help you to get a base to start with. Manual is the final and best way. Converters would help you greatly in converting. Use it, try and post specific if you get stuck.
 
Share this answer
 
For a linq query, the syntax is the same, all you should have to do is remove the line continuation characters at the end of the lines, change the key words to lower case (From => from, Into => into) and combine some keywords together (Order By => orderby, Group By => groupby ). Of course need to end the query with the c# ";". In fact, you can probably just remove the line continuation characters in VB since they are only required when there could be two meanings without the line continuation.

For a simple linq query in VB:

VB
Dim customersByCountry = From cust In customers
                         Order By cust.Country, cust.City
                         Group By CountryName = cust.Country
                         Into RegionalCustomers = Group, Count()
                         Order By CountryName


The only change would be to change the Dim to var:

C#
var customersByCountry = from cust in customers
                         orderby cust.Country, cust.City
                         groupby CountryName = cust.Country
                         into RegionalCustomers = Group, Count()
                         orderby CountryName;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900