score:0
if you're using .net 4 you could return a dynamic instead of a string and grab both values directly from the returned object.
score:0
how about returning it as tuples as long as you dont mind using the retunred type this way tuple.item1, tuple.item2
score:0
you can use the hashtable to evade creating the new result-class. something like this:
public static hashtable getaccount(int accountid)
{
linqsqldatacontext contextload = new linqsqldatacontext();
var q = (from p in contextload.myaccounts
where p.accountid == accountid
select new { name = p.firstname, surname = p.surname }).single();
return new hashtable(q.firstname, q.surname);
}
than you can get the surname by your firstname as key.
score:1
pass in two objects by reference and you can just set them.
altered to make it a try function as an example of a less code smell version
public static bool trygetaccount(int accountid, out string firstname, out string surname)
{
linqsqldatacontext contextload = new linqsqldatacontext();
var q = (from p in contextload.myaccounts
where p.accountid == accountid
select new { name = p.firstname, surname = p.surname }).singleordefault();
firstname=(q==null) ? null: q.name;
surname=(q==null) ? null: q.surname;
return q!=null;
}
now you can do
string firstname;
string surname;
if (trygetaccount(id, out firstname,out surname)) {
// firstname now equals the first name and surname now equals the surname
} else {
// deal with value not found
}
score:1
yet another (not the best :) ) option is to return an array:
public static string[] getaccount(int accountid)
{
linqsqldatacontext contextload = new linqsqldatacontext();
var q = (from p in contextload.myaccounts
where p.accountid == accountid
select new { name = p.firstname, surname = p.surname }).single();
return new []{q.name, q.surname};
}
score:4
if you don't want to define a new object for your return type, you can use tuple<string, string>
.
score:6
you can return a strongly typed class, dynamic object or a tuple. i prefer to return a strongly typed class.
the problem using the dynamic
type is that you dont get
intellisense and exceptions only at runtime.
the problem with a tuple is that it does not show you what you return. you or other developers have to read the method to know whats the name and whats the surname.
sample
public class myresult
{
public string name { get; set; }
public string surname { get; set; }
}
public static myresult getaccount(int accountid)
{
linqsqldatacontext contextload = new linqsqldatacontext();
var q = (from p in contextload.myaccounts
where p.accountid == accountid
select new myresult{ name = p.firstname, surname = p.surname }).single();
return q;
}
update
i suggest to use singleordefault
instead of single
. this will make sure you
get a null
result if the account does not exist instead of throw a exception.
//
select new myresult{ name = p.firstname, surname = p.surname }).singleordefault();
//
Source: stackoverflow.com
Related Query
- return multiple values in static class
- How do I use Linq ToDictionary to return a dictionary with multiple values in the dictionary items?
- How to return values from query when joining multiple tables in Linq?
- using Single/First methods to return multiple values in linq or lambda expression
- How to return two values from single methods and set two parent class property calling function 1 time?
- returning multiple class model data from linq in list via return method in C#
- C# Linq Comparing List Values using multiple fields and return item which doesn't satisfy the condition
- How to use LINQ expressions as static members of classes in queries when the class is related multiple times to a second class
- How to use pagelist with data source as multiple tables, to display table values in mvc?
- Extension methods must be defined in a non-generic static class
- LINQ query to return distinct field values from list of objects
- Why aren't C# static class extension methods supported?
- Why is there no Linq method to return distinct values by a predicate?
- How to select multiple values from a Dictionary using Linq as simple as possible
- Using Linq to do a Contains with multiple values
- C# System.Linq.Lookup Class Removing and Adding values
- Is there a .NET queue class that allows for dequeuing multiple items at once?
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- How to make a linq Sum return null if the summed values are all null
- How do you return a default value if a LINQ to entities query returns no values
- Stored Procedure return values with linq data context
- Using Linq to Select properties of class to return IEnumerable<T>
- Does this LINQ code perform multiple lookups on the original data?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Search dictionary values and return List of keys meeting conditions
- Linq get values not shared across multiple lists
- How to find duplicate items based on multiple values using LINQ?
- How to set multiple values in a list using lambda expression?
- Doing Distinct() using base class IEqualityComparer, and still return the child class type?
- Entity Framework Core where clause with multiple values
More Query from same tag
- LINQ Union between two tables with the same fields and then returned in a collection
- Is using LINQ against a single object considered a bad practice?
- Is there a way to find the max of a linq count function?
- what to use with Dns.GetHostAddresses with First() or FirstOrDefault()
- Joining a list of classes
- IntelliSense for XElement objects with XML schema
- Linq to objects: inner query performance
- Update Linq To Entities
- Using IEnumerable FirstOrDefault within a List extension method?
- Get weekday range from two dates
- Using Linq to combine two lists and get total quantity
- Getting username from Linked tables using Linq
- Performing Sum() Operation in LINQ
- LINQ to SQL Default Value ?? nullable
- Don't know how to generate the query I need using Entity Framework 4
- Difference between binding object/properties in ListView and ListBox
- Unlimited multi level menu in mvc
- How unique is LINQ?
- How to merge two lists while adding metadata indicating value source with LINQ statement?
- GroupBy on Multiple Properties Based on Parameters
- unique LINQ to filter list by variables (case insensitive)
- How i can split comma separated string in a group using LINQ
- Type inference failed in the call to 'Join' when using anonymous type
- Get N max numbers from a List<int> using lambda expression
- Linq order by specific enum value
- select in where - Linq Query
- Select DataTable row, filtered by GUID
- Does using a LINQ statement in a foreach re-evaluate the statement on each iteration
- how to practically assign repeating objects from groups
- IQueryable with Foreach on the result