views:

3010

answers:

5
  • How do I utilize a ?: operator in the SELECT clause of a LINQ query? If this can't be done, how can I emulate one? The goal is to get a CASE block in my select clause. As you might suspect, I'm getting an error: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

  • Is this the proper way, or a sufficient way, to say "from a inner join i on a.ipid=i.id inner join u on i.uid=u.id"? If not, please provide one. Thanks.

    var query =
     from a in db.tblActivities
     from i in db.tblIPs
     from u in db.tblUsers 
     select new {
      u.UserName == null
       ? i.Address
       : u.UserName,
      a.Request,
      a.DateTime };
    
A: 

I'm fairly new to Linq to SQL but I'm pretty sure it would go like this:

var query =
    from a in db.tblActivities
    from i in a.tblIPs
    from u in i.tblUsers 
    select new
    {
     userName = (u.UserName == null)
      ? i.Address
      : u.UserName,
     a.Request,
     a.DateTime
    };

The if statement needs to be in parentheses and the results outside of them. As for the joins, you follow the chain down from one->many.

Jason Lepack
+1  A: 

You have to use the join keyword, and define the relationship between the entities in order to make a proper inner join.

Here you can find some examples about that, I also highly recommend you to get LinqPad, its a really valuable tool for testing your queries, also its very good to learn, it has 200+ examples.

CMS
Thanks... +1, never heard of Linqpad, very handy; thanks!
tsilb
A: 

if you're checking just for null, you can also use ??

string something = null;
string somethingElse = something ?? "default value";

As for the examples above, it is correct to do the ones that go...

string something = (somethingElse == null ? "If it is true" : "if it is false");

The parens aren't required, but they do aid in reading.

Hugoware
Your logic is good for standard string manipulation, but this LINQ only halfway works the way one intuitively expects... Same error.
tsilb
weird... I've used this in LINQ queries before... the second part that is.
Hugoware
+7  A: 

When creating an anonymous type (what you're doing with the "new" without specifying a type) you have to specify the member name for each property. From your example, it would look something like this: (also fixed your joins)

var query = from a in db.tblActivities
            join i in db.tblIPs on a.ipid equals i.id
            join u in db.tblUsers on i.uid equals u.id
            select new {
               UserName = (u.UserName ?? i.Address),
               Request = a.Request,
               Date = a.DateTime
            };

You could probably do the UserName your way, too:

UserName = (u.UserName == null) ? i.Address : u.UserName,

but the ?? operator is more concise. It's similar to "isnull" in SQL.

GalacticCowboy
Awesome. That was very close, and got me to the answer:from a in TblActivitiesjoin i in TblIPs on a.IPID equals i.IDjoin u in TblUsers on a.UID equals u.IDselect new { UserName = (u.UserName ?? i.Address), Request = a.Request, Date = a.DateTime }
tsilb
If you see my example you can skip the join statements by using the table aliases instead.
Jason Lepack
Without the joins, you've just implemented a Cartesian join (cross product). If each table had 10 rows, you'd get 1000 results back. Believe me, you REALLY want those joins to be there.
GalacticCowboy
@GalacticCowboy, I'm quite certian that you're wrong. Note that the query that I proposed should use the built in FK relationships for the joins. I'm not using db on each line. See here:http://stackoverflow.com/questions/283103/linq-to-sql-foreign-keys
Jason Lepack
@Jason, You're right, I missed the fact that you were using the built-in relationships. If he were using them (and not sure why he can't...) that would work fine. I responded in your other thread as well.
GalacticCowboy
@tslib, that's a whole another question. You should certainly be handling relationships in your DB to keep your relational integrity. If you don't then you'll run into situations where data gets modified incorrectly from another source, then your RE is messed up.
Jason Lepack
@Jason, thanks for your input; but this is for my personal blog and it has no other sources.
tsilb
A: 

Really. this question depends on the particular implementation of IQueryable that your linq expression will return. I see that you have db.XXX so are you using linq to sql or some linq to data store? If so, the specific implementation of IQueryable will need to have a way to translate your expression into a store expression. Other than the above comments, some of the other comments are correct that in an anonymous type you must specify a name for each member. This is really your error.

db in this case is the DataContext for my Linq to SQL class.
tsilb
I see, that makes sense. Do you have the answer you need between all of these?