using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace ConsoleApplication1
{
public class Class1
{
static void Main(string[] args)
{
List<Car> mylist = new List<Car>();
Car car1;
Car car2;
Car car3;
car1 = new Car()
{
make = "Honda",
id = 1
};
car2 = new Car()
{
make = "toyota",
id = 2
};
car3 = new Car()
{
make = "Honda",
id = 3,
color = "red"
};
mylist.Add(car1);
mylist.Add(car2);
**////mylist.Where(p => p.id == 1).SingleOrDefault() = car3;**
}
}
public class Car
{
public int id { get; set; }
public string make { get; set; }
public string color { get; set; }
}
}
如何以最佳方式将 ID 1 的本田汽车替换为 ID 3 的本田汽车来更新列表。
leppie 所说的一切 - 加:
int index = mylist.FindIndex(p => p.id == 1);
if(index<0) {
mylist.Add(car3);
} else {
mylist[index] = car3;
}
这只是使用现有的 FindIndex 来定位 id 为 1 的汽车,然后替换或添加它。没有 LINQ;没有 SQL - 只是一个 lambda 和
List<T>
。
如果您想更新多个元素...
foreach (var f in mylist.FindAll(x => x.id == 1))
{
f.id = car3.id;
f.color = car3.color;
f.make = car3.make;
}
这不是 LINQ2SQL。
此外,LINQ 不用于更新,仅用于查询对象。
您可以这样使用:
(from car in mylist
where car.id == 1
select car).Update(
car => car.id = 3);
我的参考是这个网站。或者以下是Update方法的代码
public static void Update<T>(this IEnumerable<T> source, params Action<T>[] updates)
{
if (source == null)
throw new ArgumentNullException("source");
if (updates == null)
throw new ArgumentNullException("updates");
foreach (T item in source)
{
foreach (Action<T> update in updates)
{
update(item);
}
}
}
正如Leppie所说,LINQ是用于查询而不是更新的。但是,这可以用来构建新列表:
mylist = new List<Car>(from car in mylist select car.id == 1? car3 : car)
那就是如果你想使用LINQ。当然,这是很好且简短的代码,但比 Marc Gravell 的建议效率稍低,因为它有效地创建了一个新列表,而不是更新旧列表。
//Item class
Class Item
{
public string Name { get; set; }
}
List < Item > myList = new List< Item >()
//Add item to list
Item item = new Item();
item.Name = "Name";
myList.Add(Item);
//Find the item with the name prop
Item item2 = myList.Find(x => x.Name == "Name");
if(item2 != null)
item.Name = "Changed";
只有一个问题,为什么我必须为列表中看起来如此基本的东西编写更新函数? 列表应该有标准方法,如 Add()、Delete()、Edit()、Insert()、Replace() ....Find()
List<AvailabilityIssue> ai = new List<AvailabilityIssue>();
ai.AddRange(
(from a in db.CrewLicences
where
a.ValidationDate <= ((UniversalTime)todayDate.AddDays(30)).Time &&
a.ValidationDate >= ((UniversalTime)todayDate.AddDays(15)).Time
select new AvailabilityIssue()
{
crewMemberId = a.CrewMemberId,
expirationDays = 30,
Name = a.LicenceType.Name,
expirationDate = new UniversalTime(a.ValidationDate).ToString("yyyy-MM-dd"),
documentType = Controllers.rpmdataController.DocumentType.Licence
}).ToList());
List<AvailabilityIssue> ai = new List<AvailabilityIssue>();
ai.AddRange(
(from a in db.CrewLicences
where
a.ValidationDate <= ((UniversalTime)todayDate.AddDays(30)).Time &&
a.ValidationDate >= ((UniversalTime)todayDate.AddDays(15)).Time
select new AvailabilityIssue()
{
crewMemberId = a.CrewMemberId,
expirationDays = 30,
Name = a.LicenceType.Name,
expirationDate = new UniversalTime(a.ValidationDate).ToString("yyyy-MM-dd"),
documentType = Controllers.rpmdataController.DocumentType.Licence
}).ToList());