I have this fragment of code:
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();
I am creating new instances of ‘SmsQueues’, populating the values and when the foreach loop is finished I submit the changes. Given the new lambda/linq/anonymous types that .NET 3.5 has, is there a more ‘modern’ way to accomplish the above?
As a side question, maybe related, can I return an existing type composed of different columns in the select part of the linq expression?
Suppose you have three tables:
T1 == T1.Id, T1.Name
T2 == T2.Id, T2.Phone
T3 == T3.Name, T3.Phone, T3.SomethingElse
Can I perform a LINQ query that returns:
T1.Name, T2.Phone, SomethingElseNew
And let .NET know that that is of Type T3 (and it’s a new instance of it)? That way when I SubmitChanges, new T3 instances are inserted in the DB?
I don’t know if I make myself clear :S
I don’t have a system available to test this, but I think this (or something very close) should work.
EDIT: Fixed the numerous syntax errors (as delineated in the comments)