1

Alright, here's the deal. I am doing a data conversion where I'm taking data from two databases and putting it into another. I'm adding a list of contacts, and then I'm adding a list of communication records. In order to simplify the process, I made a small array of all of the communication records with the household address of the contacts. Now I'm trying to use a lambda expression to sort out email addresses from the array, but I'm having a problem. The code so far is as follows:

            DataRow[] Comms = dtComms.Select("household_id = " + previousID);

            if (Comms.Where(x => x.Field<string>("communication_type") == "Home Phone").Count() > 0)
            {
                string HomePhone = rNDigits.Replace(Comms[0].Field<string>("communication_value").ToString().Trim(), "");
                if (HomePhone.Length > 6)
                    oAddress._Phone = HomePhone;
            }
            if (Comms.Where(x => x.Field<string>("communication_type") == "Email").Count() > 0)
            {
                string FamilyEmail = rNDigits.Replace(Comms[0].Field<string>("communication_value").ToString().Trim(), "");
                if (FamilyEmail.Contains('@') && FamilyEmail.Contains('.'))
                    oAddress._FamilyEmail = FamilyEmail;
            }

The problem is that obviously, this always will return the first value in the array, which might not always be the one that I want. How can I change the code so that it selects only the value from the array that matches the entry containing the email? Or, is there a better way to search through values in an array?

1
  • Specifically, I want to know what I'm supposed to replace (Comms[0].Field<string>... with. Commented Sep 17, 2012 at 19:04

1 Answer 1

2

I suggesting to use a simple for or foreach loop in this case, LINQ can't modify data only select it.

Sign up to request clarification or add additional context in comments.

5 Comments

foreach makes more sense than for for most LINQ queries. Additionally it's not so much that it "can't" as it "shouldn't".
Yes you right about shouldn't. It can but in not straight forward way
Alright, looks like I'm going to rewrite a foreach loop in place of the LINQ query. Thanks, guys.
LINQ can modify data, see: stackoverflow.com/questions/807797/….
I had to wait till the time was up :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.