标签: linq-to-objects

从Int数组转换为字符串数组

当我将整数数组转换为字符串数组时,我使用for循环以较长的方式进行,如下面的示例代码中所述.这有简写吗?

现有的问题和答案SO大约int[]string(不string[]).所以他们没有帮助.

虽然我发现这个将一个int数组转换为一个String数组的答案,但该平台是Java而不是C#.同样的方法无法实现!

        int[] intarray =  { 198, 200, 354, 14, 540 };
        Array.Sort(intarray);
        string[] stringarray = { string.Empty, string.Empty, string.Empty, string.Empty, string.Empty};

        for (int i = 0; i < intarray.Length; i++)
        {
            stringarray[i] = intarray[i].ToString();
        }
Run Code Online (Sandbox Code Playgroud)

.net c# linq arrays linq-to-objects

27
推荐指数
3
解决办法
5万
查看次数

LINQ"MaxOrDefault"?

我是LINQ的新手.我需要按如下方式计算new_id:

public class C_Movement
{
  public int id=-1;
  public static ObservableCollection<C_Movement> list=new ObservableCollection<C_Movement>();
  // ...
}

int new_id = (C_Movement.list.Count==0) ? 0 : C_Movement.list.Max(x => x.id)+1;
Run Code Online (Sandbox Code Playgroud)

是否有LINQ方法来压缩该表达式,因此我不必使用"?:"结构?问题是,当C_Movement.list不包含任何元素时,C_Movement.list.Max(x => x.id)返回null(我希望它返回-1,而不是).

谢谢.

c# linq linq-to-objects

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

如何`.Take()`对一个字符串并在结尾处获取一个字符串?

LINQ to Objects支持对字符串对象的查询,但是当我使用如下代码时:

string SomeText = "this is some text in a string";
return SomeText.Take(6).ToString();
Run Code Online (Sandbox Code Playgroud)

我得到的只是:

System.Linq.Enumerable+<TakeIterator>d__3a`1[System.Char]
Run Code Online (Sandbox Code Playgroud)

这个问题中,这被视为"意外",但这正是我实际上要做的事情,我无法通过任何地方的搜索找到它.

我知道还有其他方法可以操作字符串,但后来我也知道你可以用LINQ做一些非常酷的技巧,我想知道是否有办法用LINQ修剪字符串到一定的长度?

c# linq string linq-to-objects take

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

通过列表中的两个属性选择distinct

我有一个list<message>包含类型GuidDateTime(以及其他属性)的属性.我想摆脱所有在列表中的项目,其中的GuidDateTime是相同的(除了一个).有时候这两个属性会与列表中的其他项目相同,但其他属性会有所不同,所以我不能只使用.Distinct()

List<Message> messages = GetList();
//The list now contains many objects, it is ordered by the DateTime property

messages = from p in messages.Distinct(  what goes here? ); 
Run Code Online (Sandbox Code Playgroud)

这就是我现在所拥有的,但似乎应该有更好的方法

List<Message> messages = GetList();

for(int i = 0; i < messages.Count() - 1)  //use Messages.Count() -1 because the last one has nothing after it to compare to
{
    if(messages[i].id == messages[i+1}.id && messages[i].date == message[i+1].date)
    {
        messages.RemoveAt(i+1);
    {
    else
    { …
Run Code Online (Sandbox Code Playgroud)

c# linq-to-objects list distinct

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

使用LINQ对对象列表进行分组

我有一个对象:

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int GroupID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我返回一个可能如下所示的列表:

List<Customer> CustomerList = new List<Customer>();
CustomerList.Add( new Customer { ID = 1, Name = "One", GroupID = 1 } );
CustomerList.Add( new Customer { ID = 2, Name = "Two", GroupID = 1 } );
CustomerList.Add( new Customer { ID = 3, Name = "Three", GroupID = 2 } );
CustomerList.Add( new …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-objects group-by

24
推荐指数
2
解决办法
10万
查看次数

使用LINQ to Objects查找一个集合中与另一个集合不匹配的项目

我想在一个集合中找到与另一个集合不匹配的所有项目.但是,这些集合的类型不同; 我想编写一个lambda表达式来指定相等性.

一个LINQPad我正在尝试做的例子:

void Main()
{
    var employees = new[]
    {
        new Employee { Id = 20, Name = "Bob" },
        new Employee { Id = 10, Name = "Bill" },
        new Employee { Id = 30, Name = "Frank" }
    };

    var managers = new[]
    {
        new Manager { EmployeeId = 20 },
        new Manager { EmployeeId = 30 }
    };

    var nonManagers =
    from employee in employees
    where !(managers.Any(x => x.EmployeeId == employee.Id))
    select employee;

    nonManagers.Dump(); …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-objects

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

LINQ SelectMany和Where扩展方法忽略空值

我有以下示例代码,我有兴趣知道如何使这更干净,可能通过更好地使用SelectMany().此时该QuestionList属性不会为null.我想要的只是一个answerRows非空的列表,但Questions有时也可以为null.

IEnumerable<IQuestion> questions = survey.QuestionList
                    .Where(q => q.Questions != null)
                    .SelectMany(q => q.Questions);

if(questions == null)
return null;

IEnumerable<IAnswerRow> answerRows = questions
                    .Where(q => q.AnswerRows != null)
                    .SelectMany(q => q.AnswerRows);

if(answerRows == null)
return null;
Run Code Online (Sandbox Code Playgroud)

更新: 稍微改变了我的代码,因为我的例子使用时不够清楚var

问题更多的是帮助我更多地了解LINQ的使用.

更新2:

我感兴趣的是Jon的评论Enumerable.SelectMany和Null ..所以我想尝试一些假数据的例子,以便更容易看到错误的位置,请看下面,具体我是如何使用SelectMany()a的结果SelectMany(),它更清楚我现在问题是必须确保你不使用SelectMany()空引用,当我实际读取NullReferenceException名称时显而易见:(并最终把事情放在一起.

在这样做时,我意识到try { } catch() { }在这个例子中的使用是无用的,像往常一样Jon Skeet有答案 :)延期执行..

因此,如果你想看到第2行的异常,请注释掉相关的第1行:P,抱歉,我无法弄清楚如何在不重写代码示例的情况下停止此错误.

using System;
using System.Collections.Generic;
using System.Linq;

namespace SelectManyExample …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-objects .net-4.0

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

用于连接的Lambda表达式

public class CourseDetail
    {
        public CourseDetail();
        public string CourseId { get; set; }
        public string CourseDescription { get; set; }
        public long CourseSer { get; set; }
    }

 public class RefUIDByCourse
    {
        public long CourseSer {  get;  set; }
        public double DeliveredDose{ get; set; }
        public double PlannedDose{ get; set; }
        public string RefUID {  get;  set; }
     }
 public class RefData
    {
       public double DailyDoseLimit {  get;  set; }
       public string RefName {  get;  set; }
       public string RefUID …
Run Code Online (Sandbox Code Playgroud)

c# lambda linq-to-objects

21
推荐指数
1
解决办法
7万
查看次数

LINQ to Objects连接两个集合以在第一个集合中设置值

我有以下实体框架查询:

var results = from r in db.Results
              select r;
Run Code Online (Sandbox Code Playgroud)

我正在使用AutoMapper映射到另一种类型:

var mapped = Mapper.Map<IEnumerable<Database.Result>, IEnumerable<Objects.Result>>(results);
Run Code Online (Sandbox Code Playgroud)

在我的Objects.Result类型中,我有一个名为reason的属性,它不是来自数据库.它来自另一个来源,我需要基本填充回我的映射类型:

var reasons = new List<Reason>
{
    new Reason { Id = 1, Reason = "asdf..." }
};
Run Code Online (Sandbox Code Playgroud)

我需要使用我的映射集合加入原因,并使用my reason集合中的值在我的映射集合中设置Reason属性.这可能吗?

 // need something like this:
 mapped = from m in mapped
          join r in reasons on m.Id equals r.Id
          update m.Reason = r.Reason
          select m;
Run Code Online (Sandbox Code Playgroud)

显然上面的代码没有编译,但是我能编写的代码可以做我想要的吗?

.net c# linq-to-objects

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

您最喜欢的LINQ到对象查询

使用LINQ,可以更轻松地解决许多编程问题 - 并且代码更少.

您编写的最佳实际LINQ到对象查询是什么?

(与C#2.0 /命令式方法相比,最佳=简洁和优雅).

.net linq linq-to-objects sample

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

标签 统计

linq-to-objects ×10

c# ×9

linq ×7

.net ×3

.net-4.0 ×1

arrays ×1

distinct ×1

group-by ×1

lambda ×1

list ×1

sample ×1

string ×1

take ×1