VenerableAgents
VenerableAgents

Reputation: 656

Select quoted data from query without referencing a table?

I've got a tool that uses LINQ-to-SQL and sometimes I need just one row of text data that I've provided. In the past I've done something like this:

results = (from x in [tablename] select new {myValue1 = "TextValue1", myValue2 = "TextValue2"}).Distinct();

But that requires at least one row in [tablename], which is no longer something I can rely on.

EDIT: Better example to know why I need to do this; I need this query (which is all I can edit in my program) to return everything in "results" and to be ordered correctly:

results = from z in (
 from x in [tablename]
 select new 
 {
  myValue1 = "TextValue1",
  myValue2 = "TextValue2"
 }
).Union(
 from y in [tablename]
 select new 
 {
  y.myValue1,
  y.myValue2
 }
)
orderby z.myValue1,z.myValue2
select new 
{
 z.myValue1,
 z.myValue2
};

Upvotes: 0

Views: 126

Answers (2)

Chris Farmer
Chris Farmer

Reputation: 25386

Since your method returns a regular IList, can you just do something like this?

var results = new ArrayList();
results.Add(new {myValue1 = "First", myValue2 = "First"});
results.AddRange(myLinqQuery.ToList());
return results;

...where myLinqQuery is your existing query.

Upvotes: 1

anishMarokey
anishMarokey

Reputation: 11397

for one row you can use

First() // Throws error if no value to return

or

FirstOrDefault() // wont throws error if no value to return

    results = (from x in [tablename] select new {myValue1 = 
"TextValue1", myValue2 = "TextValue2"}).Distinct().FirstOrDefault();

Upvotes: 0

Related Questions