【发布时间】:2009-03-10 14:41:49
【问题描述】:
我有这段代码:
SmsDataClassesDataContext dc = new SmsDataClassesDataContext();
// Get the customer
Customer currentCustomer = dc.Customers.Single( c => c.Hash1 == forThisHash );
// Get from Name (LINQ to XML)
var q = from c in thisSmsPack.Descendants("from")
select c;
string from = q.First().Value;
foreach ( XElement element in thisSmsPack.Descendants("to") )
{
// Create the queue
SmsQueue sq = new SmsQueue();
sq.CustomerId = currentCustomer.CustomerId;
sq.MsgFrom = from;
sq.MsgTo = element.Attribute("name").Value;
sq.MsgPhone = element.Attribute("phone").Value;
sq.MsgBody = element.Attribute("msg").Value;
sq.Priority = currentCustomer.SendsSmsAtPriority;
sq.DontSendUntil = GetNextSendDate();
// sq.TimeCreated = System.DateTime.Now;
currentCustomer.SmsQueues.Add(sq);
}
dc.SubmitChanges();
我正在创建“SmsQueues”的新实例,填充值,当 foreach 循环完成时,我提交更改。鉴于 .NET 3.5 具有新的 lambda/linq/anonymous 类型,是否有更“现代”的方式来完成上述任务?
作为一个可能相关的附带问题,我可以在 linq 表达式的选择部分返回由不同列组成的现有类型吗?
假设你有三个表:
T1 == T1.Id, T1.Name
T2 == T2.Id, T2.Phone
T3 == T3.Name, T3.Phone, T3.SomethingElse
我可以执行一个返回的 LINQ 查询吗:
T1.Name、T2.Phone、SomethingElseNew
然后让 .NET 知道它属于 T3 类型(并且它是它的新实例)? 这样当我 SubmitChanges 时,新的 T3 实例会插入到数据库中?
我不知道我是否说清楚了:S
【问题讨论】: