无法在C#中转换linq [英] Unable to Convert linq in c#

查看:85
本文介绍了无法在C#中转换linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在VB.Net中的此linq查询中尝试.

我想在C#中使用它

你能帮我吗

从e 数据库中.Stat_Elements_
                      在 db.tActors _中加入
                    打开 a.guId等于e.ActorGuid _
                 其中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)) _
                    按e.DefinitionId _分组
                   进入Antall = Count(),Verdi = Sum(e.Value * scaleFactor),AntallSolgt = Sum(e.UnitsSold)_
                      其中Antall> =  1   And  DefinitionId = definitionIdParam _
                  选择 Antall,威尔第,AntallSolgt; 

解决方案

此代码帮助我.

 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
 
命名空间汇总
{
     MyData
    {
         public  DateTime StartDate;
         public  DateTime EndDate;
        公共 字符串类型;
    }
 
     class 程序
    {
        静态 无效 Main(字符串 []参数)
        {
            列出< mydata> myDataList =  List< mydata>();
 
            myDataList.Add( MyData(){StartDate =  DateTime( 06  DateTime( 06 "   1"});
            myDataList.Add( MyData(){StartDate =  DateTime( 06  DateTime( 06 "   1"});
            myDataList.Add( MyData(){StartDate =  DateTime( 06  DateTime( 06 "   1"});
            myDataList.Add( MyData(){StartDate =  DateTime( 06  DateTime( 06 "   2"});
            myDataList.Add( MyData(){StartDate =  DateTime( 06  DateTime( 06 "   2"});
            myDataList.Add( MyData(){StartDate =  DateTime( 06  DateTime( 06 "   3"});
            myDataList.Add( MyData(){StartDate =  DateTime( 06  DateTime( 06 "   4"});
 
             var  result =
                来自记录 in  myDataList
                分组,按recod.type 放入 g
                选择 新建 {type = g.Key,StartDate = g.Min(record = >  record.EndDate)};
 
             foreach ( var  in 结果中的行)
            {
                Console.WriteLine(" ,
                   行类型
                   row.StartDate.ToString(),
                   row.EndDate.ToString());
            }
 
            Console.ReadKey();
        }
    }
} 


您是否尝试过?

看看这些工具:
.NET(C#<-> VB.NET)的代码转换 [ Developer Fusion将C#转换为VB.NET [;"结束查询.实际上,您可能只需要删除VB中的行连续字符,因为只有在可能存在两种含义而没有行连续的情况下才需要使用它们.

对于VB中的简单linq查询:

 昏暗 customersByCountry =来自客户的客户
                         按保管国,保管城市排序
                         按国家/地区分组=国家/地区
                         进入RegionalCustomers =组,Count()
                         按国家/地区名称排序



唯一的更改是将Dim 更改为var:

  var  customerByCountry = 来自客户客户
                         排序方式客户所在的国家(地区),客户所在的城市
                         groupby CountryName = cust.Country
                         进入 RegionalCustomers =组,Count()
                          orderby  CountryName; 


Hi I am trying in this linq query which is in VB.Net.

I want to use it in c#

Can you Help Me

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;

This Code Help Me.

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();  
        }  
    }  
}


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.


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:

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:

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


这篇关于无法在C#中转换linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆