First, you need to create a QueryExpression:
// Query using ConditionExpression
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "lastname";
condition1.Operator = ConditionOperator.Equal;
condition1.Values.Add("Brown");
FilterExpression filter1 = new FilterExpression();
filter1.Conditions.Add(condition1);
QueryExpression query = new QueryExpression("contact");
query.ColumnSet.AddColumns("firstname", "lastname");
query.Criteria.AddFilter(filter1);
EntityCollection result1 = _serviceProxy.RetrieveMultiple(query);
This will retrieve all the records where lastname equals "Brown", and store the results in an EntityCollection object.
You then need to retrieve the (correct) value from the entitycollection and then populate the lookup
Entity retrievedEntity = result1[0];
//This will create an account entity and add the retrieved entity ID to the primarycontact lookup field
Entity account= new Entity("account");
//Missing: add other required attributes
EntityReference primaryContactId = new EntityReference("contact", contactId);
account["primarycontactid"] = retrievedEntity.ID;
//Create the new account entity
_serviceProxy.Create(account)