小编use*_*560的帖子

将IQueryable与Linq一起使用

什么是使用IQueryable在LINQ的背景下?

它是用于开发扩展方法还是任何其他目的?

c# linq iqueryable

245
推荐指数
3
解决办法
25万
查看次数

Linq - 每组的最高价值

如何使用Linq从每个组中选择Top值

当我有一个代码段,如:

var teams = new Team[]
 { 
  new Team{PlayerName="Ricky",TeamName="Australia", PlayerScore=234},
  new Team{PlayerName="Hussy",TeamName="Australia", PlayerScore=134},
  new Team{PlayerName="Clark",TeamName="Australia", PlayerScore=334},

  new Team{PlayerName="Sankakara",TeamName="SriLanka", PlayerScore=34},
  new Team{PlayerName="Udana",TeamName="SriLanka", PlayerScore=56},
  new Team{PlayerName="Jayasurya",TeamName="SriLanka", PlayerScore=433},

 new Team{PlayerName="Flintop",TeamName="England", PlayerScore=111},
 new Team{PlayerName="Hamirson",TeamName="England", PlayerScore=13},
 new Team{PlayerName="Colingwood",TeamName="England", PlayerScore=421}
 };
Run Code Online (Sandbox Code Playgroud)

期望的结果:


Team Name         Player Name     Score

Srilanka          Jayasurya        433

England           colingwood       421

Australia         Clark            334 

c# linq lambda

38
推荐指数
4
解决办法
3万
查看次数

在LINQ中适当使用关键字"new"

请考虑以下代码:

string[] words =
{ "Too", "much", "of", "anything", "is" ,"good","for","nothing"};

var groups =from word in words
            orderby word ascending
            group word by word.Length into lengthGroups
            orderby lengthGroups.Key descending
           select new {Length=lengthGroups.Key, Words=lengthGroups};

   foreach (var group in groups)
   {
     Console.WriteLine("Words of length " + group.Length);
     foreach (string word in group.Words)
     Console.WriteLine(" " + word);
   }
Run Code Online (Sandbox Code Playgroud)

为什么我们在这里需要关键字"new"?.你能给出一些其他简单的例子来正确理解它吗?

c# linq-to-objects

4
推荐指数
1
解决办法
1734
查看次数

在Linq处理临时计算

在解决面试问题时

问题 当数字乘以2到9之间的整数时,需要以这种方式找到一个六位数字,当数字反转时给出原始的六位数字.

例:

假设我乘以219978*4我得到879912,当反向879912我将得到219978回来.

我用它解决了

for (long l = 100000; l < 999999; l++)
{
 var num = l.ToString();

for (int i = 3; i < 9; i++)
{
  var mul = l * i;
  string str = mul.ToString();
  char[] splitDigits = str.ToCharArray();
  string reversedDigit =
  new  string(splitDigits.Reverse().ToArray());

  if (reversedDigit.CompareTo(num) == 0)
    {
      Console.WriteLine("{0} * {1}= {2},
       when multiplied {3} ", num, i, mul,reversedDigit);

    }

 }
}
Run Code Online (Sandbox Code Playgroud)

最初的任务是使用linq解决它.例如,我在处理临时计算时有点困惑

当我使用

   var = from l in Enumerable.Range(100000,999999)
         from i in …
Run Code Online (Sandbox Code Playgroud)

c# linq-to-objects

4
推荐指数
1
解决办法
962
查看次数

LINQ中两个"where"之间的区别

请告诉我(1)中"where"和(2)中"where()"之间的区别.

何时使用"where"和"where()"?

List<Person> pList = 
new List<Person>
  {  
       new Person
             {EmpNo=1,FirstName="Marc",LastName="Loel",Salary=3434},

       new Person
             {EmpNo=2, FirstName="Steve",LastName="Kaith",Salary=4545},       

       new Person 
             {EmpNo=3,FirstName="Neol",LastName="Henk",Salary=2222},          

   };

 (1) var v = from p in pList where p.EmpNo == 1 select new { p.FirstName };

 (2)  var query =pList .Where(p => p.EmpNo == 1)
                               .Select(p => new { p.FirstName});
Run Code Online (Sandbox Code Playgroud)

c# linq

2
推荐指数
3
解决办法
780
查看次数

Linq化合物选择

如何在化合物选择过程中将两个数组合并到一个数组中(不使用Union)(问题在访谈时提出).

    var num1 = new int[] { 12, 3, 4, 5 };
    var num2 = new int[] { 1, 33, 6, 10 };
Run Code Online (Sandbox Code Playgroud)

我试过了

    var pairs = from a in num1 from b in num2  select new {combined={a,b}};
Run Code Online (Sandbox Code Playgroud)

预期:合并需要{12,3,4,5,1,33,6,10}

c# linq-to-objects

2
推荐指数
2
解决办法
226
查看次数

使用linq选择组合

这也是我最近面临的一个面试问题.

说明:

任务是100美元(请考虑一些货币)将给我.我需要购买三个项目项目A,项目B,项目C.成本(我不确定0.25美元或0.75美元是有意义的,所以认为其他货币)itemA = 0.25 $,itemB = 0.75 $和itemC = 20 $.我需要以100美元的价格购买100件商品(我可以购买任意数量的itemA,itemB,ItemC但总数应为100).

解决方案: 使用for循环我解决了它.

 for (int i = 1; i <= 100; i++)
   {
     for (int j = 1; j <= 100; j++)
     {
       for (int k = 1; k <= 20; k++)
        {
           if ((i * 0.25) + (j * 0.75) + (k * 5) == 100 && (i+j+k)==100)
           {
              Console.WriteLine("item1={0},item2={1},item3={2}", i, j, k);
            }
        }
     }
  }
Run Code Online (Sandbox Code Playgroud)

我也得到了结果.

item1=1 , item2=93,item3=6 // cost =100,items=100

item1=18,item2=74,item3=8 //cost=100,items=100 …
Run Code Online (Sandbox Code Playgroud)

.net linq linq-to-objects

2
推荐指数
1
解决办法
207
查看次数

使用linq解决脑筋急转弯

首先,我要向Marc Gravel,Dahlbyk和其他人表示衷心的感谢,帮助我实际应用linq.

以下是我在面试中遇到的几个问题,以解决应用Linq问题.因为我不熟悉Linq,所以我在不使用Linq的情况下解决了它.

我很欣赏能帮助我使用Linq解决问题的答案

提前致谢.


问题1: 问题是找到不同的数字,这样,无论以何种顺序使用它们来制作一个三位数的数字,该数字都不能被以下整数除尽:

3,5,7,11,13或17.

为了确保没有任何矛盾,假设三个数字是a,b和c.然后,没有数字的组合:

说abc,acb,bac,bca,cab和cba将除以3,5,7,11,13或17.

示例:

当我拿248时,它的任何组合(284,428,482,842,824)都不会被3,5,7,11,13或17整除.

public void FindingRareNumbers()
{
   for (int i = 1; i <= 9; i++)
   {
    for (int j = 1; j <= 9; j++)
    {
      for (int k = 1; k <= 9; k++)
      {
   //to form the three digit
    string digit = i.ToString() + j.ToString() + k.ToString();
   //converting to  integer
    int StrToDigit = Convert.ToInt32(digit);
    char[] digitcombination = digit.ToCharArray();
    string PossibleCombination = …
Run Code Online (Sandbox Code Playgroud)

c# linq-to-objects

1
推荐指数
1
解决办法
703
查看次数

如何从匿名类中提取属性值?

当我宣布

object o = new { name = "Bruce",Age=21 };
Console.WriteLine("name={0},age={1}",???,??? );
Run Code Online (Sandbox Code Playgroud)

现在我如何打印姓名和年龄的价值?

c# anonymous-types

0
推荐指数
1
解决办法
279
查看次数

如何进行转型?

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
string[] strings = { "zero", "one", "two", "three", "four", "five", "six",
                     "seven","eight", "nine" };

 var textNums =
                from n in numbers
                select strings[n];

   Console.WriteLine("Number strings:");

   foreach (var s in textNums)
   {
                Console.WriteLine(s);
   }
Run Code Online (Sandbox Code Playgroud)

1)将"整数"转换为表示"word"中的整数的机制是什么?

2)像int这样的转换只能用int到string吗?或者我们可以通过这种转变来玩乐吗?

c# linq-to-objects transform

0
推荐指数
2
解决办法
152
查看次数

Linq-运行总计和子总计

我尝试了一些代码来获得"运行总计"和"小计",但这确实产生了成功的结果.

当给出代码时:

var salesPeople =
new SalesPerson[] 
{ 
new SalesPerson{Name="Ricky", RegionCode="R001",SalesAmount=100,
SalesDate=Convert.ToDateTime("01/Jan/2009")},

new SalesPerson{Name="Mark", RegionCode="R001",SalesAmount=200,
SalesDate=Convert.ToDateTime("02/Jan/2009")},

new SalesPerson{Name="Jon", RegionCode="R001",SalesAmount=400,
SalesDate=Convert.ToDateTime("10/Jan/2009")},

new SalesPerson{Name="Ricky", RegionCode="R001",SalesAmount=100, 
SalesDate=Convert.ToDateTime("05/Jan/2009")},

new SalesPerson{Name="Mark", RegionCode="R001",SalesAmount=200,
SalesDate=Convert.ToDateTime("07/Jan/2009")},

new SalesPerson{Name="Jon", RegionCode="R001",SalesAmount=250,
SalesDate=Convert.ToDateTime("11/Jan/2009")},

new SalesPerson{Name="Ricky", RegionCode="R002",SalesAmount=50,
SalesDate=Convert.ToDateTime("01/feb/2009")},

new SalesPerson{Name="Mark", RegionCode="R002",SalesAmount=120,
SalesDate=Convert.ToDateTime("02/feb/2009")},

new SalesPerson{Name="Peter", RegionCode="R002",SalesAmount=30,
SalesDate=Convert.ToDateTime("10/feb/2009")},

new SalesPerson{Name="Ricky", RegionCode="R002",SalesAmount=400, 
SalesDate=Convert.ToDateTime("05/feb/2009")},

new SalesPerson{Name="Mark", RegionCode="R002",SalesAmount=70,
SalesDate=Convert.ToDateTime("07/feb/2009")},

new SalesPerson{Name="Peter", RegionCode="R002",SalesAmount=60,
SalesDate=Convert.ToDateTime("11/feb/2009")}

};
Run Code Online (Sandbox Code Playgroud)

如何找到与以下类似的Total,SubTotal,RunningTotal.

       (1) Running Total Based on Region and Month

                            Sales History
        -------------------------------------------------------------
        Region Code     Name       MonthyRunningTotal      SalesDate          
                                                           (By Month)

        -------------------------------------------------------------

         R001           Ricky        100 …
Run Code Online (Sandbox Code Playgroud)

c# linq

-1
推荐指数
1
解决办法
8000
查看次数